Modeling forecast count for MH

Log

Version 2023-03-29.
Code was initially made in SAS and send by Jemisha Apajee. File Name: “Analysis_common_diseases_step_by_step_20221123.sas”

Version 2023-04-19
Translated to R by Javier Silva-Valencia, adapted to Mental Health paper

Version 2023-05-17
Whitout social problems

Version 2023-06-07
Code debugged by Percy Soto-Becerra. The model’s problem was fixed. Diagnostic regression assessment was performed.

Context/Notes

  • This is part of a mental health paper, where we are analyzing the amount of MH-related visits among several countries (countries part of INTREPID) in a pre and during pandemic period.

  • The main variables are: Amount of total visits, amount of visits related to MH, 7-9 categories of MH groups, Tipe of visit(in-person, virtual) all this by month from 2018 to 2021 (Except Perú that has data from 2019-2021)

  • This specific code is for creating a model that fits the time series and creating a forecast to compare prepandemic and pandemic trend.

Getting ready

Load packages

Code
if (!require("pacman")) install.packages("pacman")

pacman::p_load(rio, 
               here,
               tidyverse,
               purrr, 
               patchwork, 
               scales, 
               DT, 
               janitor, 
               lubridate, 
               gtsummary, 
               broom, 
               broom.mixed,
               ggeffects, 
               lme4, 
               nlme, 
               forecast, 
               MASS, 
               mgcv, 
               tsModel,
               zoo, 
               DHARMa, 
               patchwork,
               tsibble, 
               feasts, 
               qqplotr, 
               parameters, 
               flextable,
               stringr, 
               imputeTS)

source(here("source", "diag_glmm.R"))
source(here("source", "sti_plotter_pre.R"))
source(here("source", "sti_model.R"))
source(here("source", "sti_model_china.R"))
source(here("source", "sti_model_norway.R"))
source(here("source", "sti_model_usa.R"))
source(here("source", "sti_effect_ploter.R"))

# rm(list = ls())       #To clear the environment and start from zero.
# 
# Colors' palette
cbPalette <- c("#999999",  # 1
               "#E69F00",  # 2
               "#56B4E9",  # 3
               "#009E73",  # 4
               "#F0E442",  # 5
               "#0072B2",  # 6
               "#D55E00",  # 7
               "#CC79A7")  # 8 

Read in data

Code
mh_visits <- import(here("Data", "All_countries_MHcounts.xlsx"), sheet = "MH_counts")

View data structure

Code
datatable(head(mh_visits, 100))    #Seeing first 10 rows of the dataset

Cleaning

Creating variables

Create date:

Code
mh_visits <- mh_visits %>%
  mutate(date = as.Date(paste(month, "01", year), format = "%m %d %Y")) %>% 
  arrange(country, date)

Variables of interest for Mental Healht (MH) analysis

Code
# Select variable of interest
mh_visits1 <- mh_visits |> 
  dplyr::select(country, 
         mh_category,
         year,
         month,
         date,
         den_total, 
         total_counts, 
         in_person_counts, 
         virtual_counts, 
         period)

#Sorting
mh_visits1 <- mh_visits1 %>% 
  arrange(country, date, mh_category)

# Rename columns
mh_visits1 <- mh_visits1 %>%
  rename(numerator = total_counts,         #MH counts
         denominator = den_total)          #Total visits per month

Check for missings

Code
mh_visits1 %>%
  tabyl(country, period, show_na = TRUE)           #No missings
   country pandemic1 pandemic2 pre-pandemic washout
 Argentina        99        90          234       9
 Australia        99        90          234       9
    Canada        99        90          234       9
     China       108        99          225       0
    Norway        99        90          234       9
      Peru        99        90          126       9
 Singapore        99        90          234       9
    Sweden        99        90          234       9
       USA        99        90          234       9

Sort categories of MH

Code
#Eliminando categorias eliminadas
mh_visits2 <- mh_visits1 %>% 
  filter(!(mh_category %in% c("Social Problems"))) 

# Establecer el nuevo orden para las categorías
new_order <- c("Anxiety and Mood Disorders", 
               "Bipolar, Schizophrenia and other Psycotic Disorders",
               "Dementia", 
               "ADHD and Eating Disorders", 
               "Sleep Disorders", 
               "Substance-Related and Addictive Disorders")

# Reorder categories
mh_visits2 <- mh_visits2 %>% 
  filter(mh_category %in% new_order) %>% 
  mutate(mh_category = fct_relevel(factor(mh_category), new_order))

Set up data for modelling

Code
pandemic_start       <- as.Date("2020-04-01")        #For all the countries the pandemic start april 2020 *To revise

pandemic_start_china <- as.Date("2020-02-01")  #For china pandemic start at feb 2020

mh_visits5           <- mh_visits2 %>%
  # create t = 1, ..., 48 for each country (except peru)
  mutate(
    tn = if_else(country != "Peru", 
                 (12 * (year - 2018)) + month, 
                 (12 * (year - 2019)) + month),
  time = tn, # for random effects in the residuals
  #Spliting N# visits into two variables (pre and post start of pandemic)   
  y_count = case_when(
    country != "China" & date < pandemic_start ~ numerator,
    country == "China" & date < pandemic_start_china ~ numerator,
    TRUE ~ NA_real_
    ),
  observed_count = case_when(
    country != "China" & date >= pandemic_start ~ numerator,
    country == "China" & date >= pandemic_start_china ~ numerator,
    TRUE ~ NA_real_
    ),
      
  # Dummy variables for China (changes in their health system administration)
  China_Jan19 = if_else(date >= as.Date("2019-01-01"), 1, 0),
  China_Dec20 = if_else(date >= as.Date("2020-12-01"), 1, 0),
  Norway_Feb21 = if_else(time >= 38, 1, 0), 
  USA_systdown = if_else(time >= 37 & time <= 39, 1, 0), 
  
  # Pre-post tag variable
  country = as.factor(country),
  pre_post = if_else(period == "pre-pandemic", 0, 1), 
  pre_postTo = if_else(is.na(y_count), "post", "pre"),      #pre/post binary variable
      
  #"tn" as a factor (will be a classification variable)
  tn = as.factor(tn), 
  
  #Creating variable for interaction pre-post exposure (wahsout would be on pre exposure)
  To = ifelse(period %in% c("pre-pandemic", "washout"), 0, 1), 
  
  # Creating rate variable for graphics
  rate = 10000 * numerator / denominator
  ) 

mh_visits_all <- mh_visits5 %>% 
  group_by(country, date) %>% 
  summarize(
    year = max(year),
    month = max(month),
    denominator = max(denominator),
    numerator = sum(numerator),
    observed_count = sum(observed_count),
    To = max(To), 
    time = max(time),
    period = max(period), 
    China_Jan19 = max(China_Jan19), 
    China_Dec20 = max(China_Dec20)
  ) %>%
  ungroup() |> 
  mutate(mh_category = "All common Mental Health disorders", 
         Norway_Feb21 = if_else(time >= 38, 1, 0), 
         USA_systdown = if_else(time >= 37 & time <= 39, 1, 0))

mh_visits_all  |> 
  bind_rows(mh_visits5) -> mh_visits5b

Pre-pandemic behaviour of outcomes (blinded to post-pandemic)

Code
min_tdate <- min(mh_visits5$date) #- month(1)
max_tdate <- max(mh_visits5$date) #+ month(1)

countryc <-  mh_visits5 |> 
  count(country) |> 
  pull(country) |> 
  as.character()

mh_categoryc <- mh_visits5 |> 
  count(mh_category) |> 
  pull(mh_category) |> 
  as.character()
Code
combinations <- expand.grid(country = countryc, mh_category = mh_categoryc)

combinations <- combinations %>%
  mutate(plot = map2(country, mh_category, ~ 
                       sti_plotter_pre(.x, .y, 
                                       blind = FALSE, 
                                       data = mh_visits5))) %>%
  pwalk(~ ggsave(filename = paste0(..1, "_", ..2, ".png"), 
                 plot = ..3, 
                 device = "png", 
                 path = here("img", "exploratory_plots"), 
                 scale = 2, 
                 width = 7, 
                 height = 7, 
                 units = "cm", 
                 dpi = 900
                 ))

# Extrae la lista de gráficos
plots_list <- combinations$plot
Code
i <- 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Creating and Testing the Model

Code
# Countries and categories
countryc <-  mh_visits5b |> 
  count(country) |> 
  pull(country) |> 
  as.character()

countryc <- rep(countryc, 7)


mh_categoryc <- mh_visits5b |> 
  mutate(
    mh_category = 
      factor(mh_category, 
             levels = c("All common Mental Health disorders", 
                        "Anxiety and Mood Disorders", 
                        "ADHD and Eating Disorders", 
                        "Bipolar, Schizophrenia and other Psycotic Disorders", 
                        "Dementia", 
                        "Sleep Disorders", 
                        "Substance-Related and Addictive Disorders"))) |> 
  count(mh_category) |> 
  pull(mh_category) |> 
  as.character()

Modelling

Set global options

Code
# set options of graphics
alfa0 <- 0.5
alfa_ribbon0 <- 0.25
color_point0 <- c("#99B933")
color_line0 <- c("#002D72")
color_interrupt0 <- c("red")
color_ribbon_ex0 <- c("skyblue")
color_ribbon_def0 <- c("orange")
mindate0 <- "2018-01-01"
maxdate0 <- "2022-01-01"

Outcome: All common Mental Health disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
i <- 0
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.03
AIC=140.06   AICc=140.15   BIC=141.93
Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.03
AIC=140.06   AICc=140.15   BIC=141.93
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.932079e-07 (Intr)
time        5.906248e-09 0     
Residual    2.683836e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.16082929  0.08349518  0.10144566 -0.08609451 -0.32118000 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -3.452477 0.02188146 40 -157.78091  0.0000
time                    -0.004964 0.00137534 40   -3.60898  0.0008
To                       0.109672 0.03503510 40    3.13033  0.0033
harmonic(month, 2, 12)1 -0.033729 0.01991114 40   -1.69400  0.0980
harmonic(month, 2, 12)2  0.042649 0.00824037 40    5.17556  0.0000
harmonic(month, 2, 12)3  0.027202 0.01976296 40    1.37644  0.1763
harmonic(month, 2, 12)4  0.033617 0.00829591 40    4.05222  0.0002
To:I(time - tpand)       0.001727 0.00249365 40    0.69266  0.4925
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.903                                            
To                       0.417 -0.613                                     
harmonic(month, 2, 12)1 -0.101  0.110 -0.090                              
harmonic(month, 2, 12)2 -0.005 -0.026  0.084  0.010                       
harmonic(month, 2, 12)3  0.104 -0.108  0.136 -0.007     0.029             
harmonic(month, 2, 12)4 -0.009  0.026 -0.029 -0.093    -0.030     0.011   
To:I(time - tpand)       0.443 -0.468 -0.293  0.041    -0.020    -0.027   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.038   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.18868406 -0.73664106  0.05067041  0.61414783  2.21978444 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab0 |> 
  knitr::kable()
country mh_category effect estimate
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.116:  log likelihood = -70.75
AIC=143.5   AICc=143.59   BIC=145.37
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.946789e-07 (Intr)
time        5.680489e-09 0     
Residual    3.696840e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.05932616 -0.30738443  0.09328414 -0.25808594 -0.28097780 -0.44210280 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -3.330240 0.011270987 40 -295.47011  0.0000
time                    -0.000602 0.000694956 40   -0.86688  0.3912
To                       0.115781 0.016118523 40    7.18309  0.0000
harmonic(month, 2, 12)1 -0.033861 0.014614855 40   -2.31688  0.0257
harmonic(month, 2, 12)2  0.046119 0.006630108 40    6.95593  0.0000
harmonic(month, 2, 12)3  0.033354 0.015443595 40    2.15975  0.0368
harmonic(month, 2, 12)4 -0.030540 0.006656039 40   -4.58825  0.0000
To:I(time - tpand)      -0.003572 0.001088861 40   -3.28051  0.0022
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.917                                            
To                       0.498 -0.699                                     
harmonic(month, 2, 12)1 -0.024  0.043 -0.078                              
harmonic(month, 2, 12)2 -0.029  0.003  0.046 -0.041                       
harmonic(month, 2, 12)3  0.178 -0.150  0.094  0.014     0.038             
harmonic(month, 2, 12)4 -0.004  0.022 -0.018  0.001    -0.005    -0.025   
To:I(time - tpand)       0.488 -0.487 -0.170  0.083    -0.015     0.111   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.3357351 -0.5897083  0.1865211  0.8621850  1.6078218 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.5420
s.e.  0.1117

sigma^2 = 1.038:  log likelihood = -68.67
AIC=141.34   AICc=141.61   BIC=145.09
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.788071e-07 (Intr)
time        8.196031e-09 0     
Residual    4.052217e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.72282181 -0.41456721  0.16147478 -0.13912600  0.04050206 -0.10468933 
       Phi7        Phi8        Phi9       Phi10       Phi11 
-0.05130863  0.05399622  0.03042363 -0.02024095 -0.31233934 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.3530457 0.020081536 40 -117.17459  0.0000
time                     0.0055529 0.001257661 40    4.41529  0.0001
To                       0.1943678 0.030431593 40    6.38704  0.0000
harmonic(month, 2, 12)1  0.0474961 0.010296480 40    4.61284  0.0000
harmonic(month, 2, 12)2 -0.0170364 0.007081764 40   -2.40567  0.0209
harmonic(month, 2, 12)3 -0.0081269 0.010546546 40   -0.77057  0.4455
harmonic(month, 2, 12)4 -0.0131689 0.007003002 40   -1.88046  0.0673
To:I(time - tpand)      -0.0061013 0.002182289 40   -2.79585  0.0079
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.924                                            
To                       0.468 -0.662                                     
harmonic(month, 2, 12)1 -0.131  0.095 -0.070                              
harmonic(month, 2, 12)2  0.016 -0.052  0.134 -0.015                       
harmonic(month, 2, 12)3  0.096 -0.148  0.323 -0.019     0.026             
harmonic(month, 2, 12)4 -0.068  0.088 -0.075 -0.018    -0.049    -0.046   
To:I(time - tpand)       0.474 -0.464 -0.263  0.059    -0.050    -0.205   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.059   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1808627 -0.5387176 -0.2422736  0.5147350  3.2885588 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,2) with zero mean 

Coefficients:
          ar1      ar2     ma1     ma2
      -1.2222  -0.8008  1.0188  0.4257
s.e.   0.1375   0.1185  0.2284  0.1888

sigma^2 = 1.002:  log likelihood = -66.49
AIC=142.99   AICc=144.42   BIC=152.34
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.609707e-07 (Intr)
time        1.237496e-09 0     
Residual    3.749607e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-1.0188105 -0.5852453  0.6676290 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -3.358373 0.07415377 38 -45.28932  0.0000
time                    -0.024137 0.00943317 38  -2.55875  0.0146
To                       0.749113 0.11381639 38   6.58177  0.0000
harmonic(month, 2, 12)1 -0.096351 0.04046236 38  -2.38126  0.0224
harmonic(month, 2, 12)2 -0.089169 0.02921778 38  -3.05188  0.0041
harmonic(month, 2, 12)3  0.051363 0.02443203 38   2.10230  0.0422
harmonic(month, 2, 12)4  0.068126 0.02793228 38   2.43898  0.0195
China_Jan19              1.538481 0.12965093 38  11.86633  0.0000
China_Dec20             -0.525973 0.14435416 38  -3.64363  0.0008
To:I(time - tpand)      -0.050294 0.01065822 38  -4.71877  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.806                                            
To                       0.688 -0.848                                     
harmonic(month, 2, 12)1 -0.568  0.701 -0.741                              
harmonic(month, 2, 12)2 -0.293  0.369 -0.365  0.380                       
harmonic(month, 2, 12)3  0.288 -0.351  0.365 -0.217    -0.097             
harmonic(month, 2, 12)4  0.211 -0.263  0.227 -0.159    -0.077     0.114   
China_Jan19              0.539 -0.921  0.737 -0.647    -0.345     0.312   
China_Dec20              0.417 -0.516  0.696 -0.719    -0.381     0.149   
To:I(time - tpand)       0.193 -0.242 -0.189  0.241     0.114     0.053   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.230                 
China_Dec20              0.059     0.476       
To:I(time - tpand)       0.124     0.224 -0.632

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7765779 -0.6266211 -0.1253249  0.3924598  2.9611792 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Notes
  • Report in text (maybe in supplementary table?) that including period of shock (t > Dec 2020) does not significantly change the results.
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,2) with zero mean 

Coefficients:
         ma1      ma2
      0.7608  -0.1839
s.e.  0.1316   0.1148

sigma^2 = 0.807:  log likelihood = -62.99
AIC=131.98   AICc=132.52   BIC=137.59
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 9.977317e-07 (Intr)
time        2.010650e-08 0     
Residual    1.251786e+01       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.4381155 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value  Std.Error DF   t-value p-value
(Intercept)             -2.4763271 0.02490478 40 -99.43179  0.0000
time                     0.0015374 0.00148448 40   1.03565  0.3066
To                       0.1115606 0.03227661 40   3.45639  0.0013
harmonic(month, 2, 12)1  0.0119611 0.01177849 40   1.01550  0.3160
harmonic(month, 2, 12)2 -0.0098213 0.00899146 40  -1.09229  0.2812
harmonic(month, 2, 12)3  0.0328905 0.01192776 40   2.75747  0.0087
harmonic(month, 2, 12)4  0.0026575 0.00877545 40   0.30283  0.7636
To:I(time - tpand)      -0.0097823 0.00272919 40  -3.58433  0.0009
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.860                                            
To                       0.277 -0.524                                     
harmonic(month, 2, 12)1 -0.102  0.055 -0.008                              
harmonic(month, 2, 12)2 -0.021  0.003  0.092 -0.052                       
harmonic(month, 2, 12)3 -0.010 -0.062  0.241  0.018    -0.015             
harmonic(month, 2, 12)4 -0.029  0.067 -0.080 -0.038    -0.014    -0.063   
To:I(time - tpand)       0.511 -0.621 -0.171  0.040    -0.032    -0.152   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.045   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.5312363 -0.7411466  0.1436895  0.7614220  1.8328748 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway All common Mental Health disorders Level change 1.118 (1.053 to 1.187), p = 0.001
Norway All common Mental Health disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.4596
s.e.  0.1719

sigma^2 = 0.9012:  log likelihood = -48.82
AIC=101.64   AICc=102.01   BIC=104.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.569990e-07 (Intr)
time        3.451858e-09 0     
Residual    1.817722e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.09008322 -0.14179720 -0.15816761 -0.61118340 -0.45445075 -0.16982838 
       Phi7        Phi8 
 0.17268850 -0.39894690 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -4.043470 0.013142243 28 -307.66972  0.0000
time                     0.025395 0.001418969 28   17.89681  0.0000
To                       0.749106 0.013426691 28   55.79228  0.0000
harmonic(month, 2, 12)1 -0.022995 0.008420245 28   -2.73088  0.0108
harmonic(month, 2, 12)2  0.005221 0.006982622 28    0.74769  0.4609
harmonic(month, 2, 12)3 -0.053847 0.009449809 28   -5.69821  0.0000
harmonic(month, 2, 12)4  0.018314 0.006980597 28    2.62357  0.0139
To:I(time - tpand)      -0.029406 0.001438343 28  -20.44432  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.962                                            
To                       0.710 -0.840                                     
harmonic(month, 2, 12)1  0.042 -0.007 -0.054                              
harmonic(month, 2, 12)2 -0.012 -0.015  0.044 -0.095                       
harmonic(month, 2, 12)3  0.349 -0.338  0.172  0.026     0.025             
harmonic(month, 2, 12)4 -0.099  0.089 -0.022  0.026    -0.034    -0.144   
To:I(time - tpand)       0.919 -0.935  0.631  0.052     0.011     0.415   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.133   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.14035315 -0.35776236  0.08254759  0.51470508  1.76779570 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru All common Mental Health disorders Level change 2.115 (2.064 to 2.167), p < 0.001
Peru All common Mental Health disorders Trend change 0.971 (0.969 to 0.974), p < 0.001
Norway All common Mental Health disorders Level change 1.118 (1.053 to 1.187), p = 0.001
Norway All common Mental Health disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.038:  log likelihood = -69
AIC=140   AICc=140.09   BIC=141.87
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.195175e-07 (Intr)
time        6.054890e-09 0     
Residual    1.348695e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.04959712 -0.06154859 -0.06379853  0.17909585 -0.17813190 -0.09681921 
       Phi7        Phi8        Phi9       Phi10       Phi11 
 0.05416645 -0.06537127 -0.12159967 -0.08546415 -0.40868122 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.710014 0.02385837 40 -239.32962  0.0000
time                     0.008747 0.00155266 40    5.63367  0.0000
To                       0.206288 0.04258769 40    4.84385  0.0000
harmonic(month, 2, 12)1 -0.023229 0.01082195 40   -2.14647  0.0380
harmonic(month, 2, 12)2  0.007227 0.01100509 40    0.65673  0.5151
harmonic(month, 2, 12)3  0.011564 0.01153176 40    1.00281  0.3220
harmonic(month, 2, 12)4 -0.009629 0.01085228 40   -0.88732  0.3802
To:I(time - tpand)       0.010027 0.00244761 40    4.09663  0.0002
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.936                                            
To                       0.564 -0.739                                     
harmonic(month, 2, 12)1 -0.127  0.108 -0.079                              
harmonic(month, 2, 12)2 -0.020 -0.012  0.046  0.029                       
harmonic(month, 2, 12)3  0.177 -0.247  0.422 -0.069     0.058             
harmonic(month, 2, 12)4 -0.010  0.020  0.001 -0.073    -0.046    -0.017   
To:I(time - tpand)       0.350 -0.288 -0.365  0.059    -0.008    -0.288   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1750600 -0.7130761 -0.2588606  0.3983306  2.4580597 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore All common Mental Health disorders Level change 1.229 (1.136 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.006 to 1.015), p < 0.001
Peru All common Mental Health disorders Level change 2.115 (2.064 to 2.167), p < 0.001
Peru All common Mental Health disorders Trend change 0.971 (0.969 to 0.974), p < 0.001
Norway All common Mental Health disorders Level change 1.118 (1.053 to 1.187), p = 0.001
Norway All common Mental Health disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.041:  log likelihood = -69.06
AIC=140.13   AICc=140.21   BIC=142
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.858397e-07 (Intr)
time        4.231727e-09 0     
Residual    2.657486e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.072690433  0.158082444  0.008351295  0.027148432  0.035154355 -0.627804579 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.2875658 0.014318108 40 -159.76732  0.0000
time                     0.0054323 0.000878043 40    6.18679  0.0000
To                       0.0706213 0.021353481 40    3.30725  0.0020
harmonic(month, 2, 12)1  0.0189794 0.018194760 40    1.04313  0.3032
harmonic(month, 2, 12)2 -0.0287076 0.004687890 40   -6.12379  0.0000
harmonic(month, 2, 12)3  0.0184154 0.018095526 40    1.01767  0.3149
harmonic(month, 2, 12)4  0.0076209 0.004578440 40    1.66453  0.1038
To:I(time - tpand)       0.0014019 0.001467138 40    0.95555  0.3450
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.436 -0.647                                     
harmonic(month, 2, 12)1 -0.120  0.086 -0.033                              
harmonic(month, 2, 12)2  0.042 -0.062  0.120 -0.081                       
harmonic(month, 2, 12)3 -0.059  0.003  0.114  0.036    -0.060             
harmonic(month, 2, 12)4  0.004  0.035 -0.055 -0.057     0.002    -0.028   
To:I(time - tpand)       0.488 -0.489 -0.239 -0.013    -0.001    -0.163   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.024   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-3.28170355 -0.61370874 -0.03938239  0.64695097  2.04930975 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden All common Mental Health disorders Level change 1.073 (1.032 to 1.116), p = 0.002
Sweden All common Mental Health disorders Trend change 1.001 (0.999 to 1.004), p = 0.345
Singapore All common Mental Health disorders Level change 1.229 (1.136 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.006 to 1.015), p < 0.001
Peru All common Mental Health disorders Level change 2.115 (2.064 to 2.167), p < 0.001
Peru All common Mental Health disorders Trend change 0.971 (0.969 to 0.974), p < 0.001
Norway All common Mental Health disorders Level change 1.118 (1.053 to 1.187), p = 0.001
Norway All common Mental Health disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.051:  log likelihood = -69.31
AIC=140.63   AICc=140.71   BIC=142.5
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.990877e-07 (Intr)
time        8.526998e-09 0     
Residual    2.195266e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
0.02661089 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -1.7839168 0.017062011 40 -104.55490  0.0000
time                     0.0022179 0.001078498 40    2.05644  0.0463
To                      -0.0277751 0.026998490 40   -1.02877  0.3098
harmonic(month, 2, 12)1  0.0394802 0.009114229 40    4.33171  0.0001
harmonic(month, 2, 12)2  0.0018055 0.008850194 40    0.20401  0.8394
harmonic(month, 2, 12)3 -0.0162941 0.009276194 40   -1.75655  0.0866
harmonic(month, 2, 12)4 -0.0055444 0.008900138 40   -0.62296  0.5368
To:I(time - tpand)       0.0045977 0.001957279 40    2.34902  0.0238
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.868                                            
To                       0.356 -0.592                                     
harmonic(month, 2, 12)1 -0.143  0.100 -0.052                              
harmonic(month, 2, 12)2 -0.038  0.002  0.032  0.002                       
harmonic(month, 2, 12)3  0.099 -0.144  0.251 -0.032    -0.015             
harmonic(month, 2, 12)4  0.017  0.005 -0.023 -0.005    -0.018     0.001   
To:I(time - tpand)       0.449 -0.519 -0.243  0.038     0.016    -0.099   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.014   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1330418 -0.5504796  0.1434106  0.6240550  2.3335762 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA All common Mental Health disorders Level change 0.973 (0.925 to 1.022), p = 0.31
USA All common Mental Health disorders Trend change 1.005 (1.001 to 1.008), p = 0.024
Sweden All common Mental Health disorders Level change 1.073 (1.032 to 1.116), p = 0.002
Sweden All common Mental Health disorders Trend change 1.001 (0.999 to 1.004), p = 0.345
Singapore All common Mental Health disorders Level change 1.229 (1.136 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.006 to 1.015), p < 0.001
Peru All common Mental Health disorders Level change 2.115 (2.064 to 2.167), p < 0.001
Peru All common Mental Health disorders Trend change 0.971 (0.969 to 0.974), p < 0.001
Norway All common Mental Health disorders Level change 1.118 (1.053 to 1.187), p = 0.001
Norway All common Mental Health disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China All common Mental Health disorders Level change 2.115 (1.723 to 2.596), p < 0.001
China All common Mental Health disorders Trend change 0.951 (0.933 to 0.969), p < 0.001
Canada All common Mental Health disorders Level change 1.215 (1.148 to 1.285), p < 0.001
Canada All common Mental Health disorders Trend change 0.994 (0.99 to 0.998), p = 0.008
Australia All common Mental Health disorders Level change 1.123 (1.09 to 1.157), p < 0.001
Australia All common Mental Health disorders Trend change 0.996 (0.994 to 0.998), p = 0.002
Argentina All common Mental Health disorders Level change 1.116 (1.046 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1.002 (0.997 to 1.006), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.116 (1.046 to 1.19), p = 0.003

Trend change

1.002 (0.997 to 1.006), p = 0.493

Australia

Level change

1.123 (1.09 to 1.157), p < 0.001

Trend change

0.996 (0.994 to 0.998), p = 0.002

Canada

Level change

1.215 (1.148 to 1.285), p < 0.001

Trend change

0.994 (0.99 to 0.998), p = 0.008

China

Level change

2.115 (1.723 to 2.596), p < 0.001

Trend change

0.951 (0.933 to 0.969), p < 0.001

Norway

Level change

1.118 (1.053 to 1.187), p = 0.001

Trend change

0.99 (0.985 to 0.995), p < 0.001

Peru

Level change

2.115 (2.064 to 2.167), p < 0.001

Trend change

0.971 (0.969 to 0.974), p < 0.001

Singapore

Level change

1.229 (1.136 to 1.33), p < 0.001

Trend change

1.01 (1.006 to 1.015), p < 0.001

Sweden

Level change

1.073 (1.032 to 1.116), p = 0.002

Trend change

1.001 (0.999 to 1.004), p = 0.345

USA

Level change

0.973 (0.925 to 1.022), p = 0.31

Trend change

1.005 (1.001 to 1.008), p = 0.024

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Anxiety and Mood Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.061:  log likelihood = -69.52
AIC=141.04   AICc=141.13   BIC=142.91
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.591735e-07 (Intr)
time        8.006211e-09 0     
Residual    2.545325e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.16138058  0.09500984  0.16674955 -0.08548189 -0.43256855 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -3.800860 0.02323842 40 -163.55930   0.000
time                    -0.008031 0.00147007 40   -5.46307   0.000
To                       0.207874 0.03702527 40    5.61438   0.000
harmonic(month, 2, 12)1 -0.028836 0.02545260 40   -1.13292   0.264
harmonic(month, 2, 12)2  0.041202 0.00790594 40    5.21147   0.000
harmonic(month, 2, 12)3  0.018212 0.02513936 40    0.72445   0.473
harmonic(month, 2, 12)4  0.028253 0.00795516 40    3.55154   0.001
To:I(time - tpand)       0.005824 0.00262111 40    2.22184   0.032
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.904                                            
To                       0.427 -0.625                                     
harmonic(month, 2, 12)1 -0.103  0.115 -0.092                              
harmonic(month, 2, 12)2 -0.003 -0.030  0.097  0.036                       
harmonic(month, 2, 12)3  0.087 -0.079  0.101  0.000     0.031             
harmonic(month, 2, 12)4 -0.008  0.027 -0.031 -0.111    -0.035     0.038   
To:I(time - tpand)       0.452 -0.475 -0.275  0.032    -0.026    -0.025   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.042   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.35755543 -0.66320592  0.03942071  0.65657986  2.32936383 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,1) with zero mean 

Coefficients:
          ar1     ma1
      -0.4397  0.8526
s.e.   0.1980  0.1227

sigma^2 = 0.9512:  log likelihood = -66.17
AIC=138.35   AICc=138.89   BIC=143.96
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.181207e-07 (Intr)
time        3.969713e-09 0     
Residual    3.365618e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
 0.1335535 -0.3340768  0.1096485 -0.2789437 -0.2507490 -0.4412085 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -3.612990 0.011792214 40 -306.38773  0.0000
time                    -0.000813 0.000726960 40   -1.11835  0.2701
To                       0.092133 0.016946200 40    5.43678  0.0000
harmonic(month, 2, 12)1 -0.039993 0.016003698 40   -2.49897  0.0167
harmonic(month, 2, 12)2  0.040256 0.007000316 40    5.75057  0.0000
harmonic(month, 2, 12)3  0.026395 0.016920312 40    1.55997  0.1266
harmonic(month, 2, 12)4 -0.030266 0.007026692 40   -4.30736  0.0001
To:I(time - tpand)      -0.004327 0.001152668 40   -3.75361  0.0006
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.917                                            
To                       0.496 -0.696                                     
harmonic(month, 2, 12)1 -0.018  0.041 -0.077                              
harmonic(month, 2, 12)2 -0.028  0.002  0.047 -0.038                       
harmonic(month, 2, 12)3  0.186 -0.154  0.091  0.017     0.040             
harmonic(month, 2, 12)4 -0.005  0.023 -0.018 -0.002    -0.006    -0.020   
To:I(time - tpand)       0.484 -0.482 -0.180  0.082    -0.016     0.119   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.039   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.1642999 -0.7017196  0.1560169  0.9089529  1.5773536 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.5932
s.e.  0.1179

sigma^2 = 0.9968:  log likelihood = -67.74
AIC=139.49   AICc=139.75   BIC=143.23
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.433539e-07 (Intr)
time        5.929259e-09 0     
Residual    3.564579e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.584123970 -0.374846652  0.113012997 -0.230500244  0.083003896 -0.183975516 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.127570443  0.049103025  0.002047399 -0.018200509 -0.315852719 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.6018930 0.016344447 40 -159.19125  0.0000
time                     0.0046973 0.001043251 40    4.50252  0.0001
To                       0.2492474 0.026154294 40    9.52988  0.0000
harmonic(month, 2, 12)1  0.0479674 0.011770720 40    4.07515  0.0002
harmonic(month, 2, 12)2 -0.0161088 0.007489049 40   -2.15099  0.0376
harmonic(month, 2, 12)3 -0.0048958 0.011788032 40   -0.41532  0.6801
harmonic(month, 2, 12)4 -0.0135749 0.007430823 40   -1.82684  0.0752
To:I(time - tpand)      -0.0066214 0.001665934 40   -3.97461  0.0003
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.929                                            
To                       0.539 -0.723                                     
harmonic(month, 2, 12)1 -0.169  0.143 -0.121                              
harmonic(month, 2, 12)2  0.000 -0.026  0.083 -0.011                       
harmonic(month, 2, 12)3  0.099 -0.136  0.259 -0.021     0.009             
harmonic(month, 2, 12)4 -0.039  0.051 -0.034 -0.020    -0.042    -0.027   
To:I(time - tpand)       0.416 -0.385 -0.273  0.058    -0.033    -0.176   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.057   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1121423 -0.6024484 -0.1166695  0.5957475  3.8567171 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,2) with zero mean 

Coefficients:
          ar1      ar2     ma1     ma2
      -1.2499  -0.8166  1.1240  0.4632
s.e.   0.1352   0.1075  0.2389  0.1966

sigma^2 = 0.9488:  log likelihood = -65.29
AIC=140.57   AICc=142   BIC=149.93
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.366316e-07 (Intr)
time        1.948769e-09 0     
Residual    3.037535e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-1.0261210 -0.6383108  0.6905835 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -3.915050 0.07366930 38 -53.14358  0.0000
time                    -0.030274 0.00921657 38  -3.28469  0.0022
To                       0.968990 0.11242260 38   8.61918  0.0000
harmonic(month, 2, 12)1 -0.149789 0.03986458 38  -3.75743  0.0006
harmonic(month, 2, 12)2 -0.092598 0.02882962 38  -3.21191  0.0027
harmonic(month, 2, 12)3  0.057361 0.02403005 38   2.38707  0.0221
harmonic(month, 2, 12)4  0.099955 0.02767052 38   3.61234  0.0009
China_Jan19              1.724061 0.12721735 38  13.55209  0.0000
China_Dec20             -0.426280 0.14254494 38  -2.99050  0.0049
To:I(time - tpand)      -0.074245 0.01067461 38  -6.95525  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.792                                            
To                       0.676 -0.849                                     
harmonic(month, 2, 12)1 -0.553  0.698 -0.747                              
harmonic(month, 2, 12)2 -0.290  0.371 -0.364  0.377                       
harmonic(month, 2, 12)3  0.282 -0.350  0.365 -0.214    -0.093             
harmonic(month, 2, 12)4  0.196 -0.252  0.218 -0.153    -0.073     0.125   
China_Jan19              0.506 -0.915  0.735 -0.640    -0.343     0.309   
China_Dec20              0.400 -0.506  0.695 -0.712    -0.368     0.129   
To:I(time - tpand)       0.177 -0.224 -0.212  0.259     0.108     0.055   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.220                 
China_Dec20              0.043     0.464       
To:I(time - tpand)       0.119     0.206 -0.647

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.8328969 -0.5905426 -0.1522497  0.5191465  2.6472425 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.4615
s.e.  0.1349

sigma^2 = 0.9024:  log likelihood = -65.26
AIC=134.52   AICc=134.79   BIC=138.26
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.494196e-07 (Intr)
time        2.541074e-08 0     
Residual    1.077307e+01       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.3555671 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value  Std.Error DF    t-value p-value
(Intercept)             -2.8318031 0.02374881 40 -119.23980  0.0000
time                     0.0016732 0.00142840 40    1.17137  0.2484
To                       0.1009929 0.03214417 40    3.14187  0.0032
harmonic(month, 2, 12)1  0.0246859 0.01160635 40    2.12693  0.0396
harmonic(month, 2, 12)2 -0.0179669 0.00950224 40   -1.89081  0.0659
harmonic(month, 2, 12)3  0.0357891 0.01184724 40    3.02088  0.0044
harmonic(month, 2, 12)4 -0.0040039 0.00928323 40   -0.43130  0.6686
To:I(time - tpand)      -0.0102951 0.00259753 40   -3.96340  0.0003
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.863                                            
To                       0.298 -0.547                                     
harmonic(month, 2, 12)1 -0.111  0.061 -0.019                              
harmonic(month, 2, 12)2 -0.019  0.003  0.081 -0.055                       
harmonic(month, 2, 12)3  0.009 -0.083  0.247  0.022    -0.023             
harmonic(month, 2, 12)4 -0.020  0.058 -0.072 -0.029    -0.010    -0.061   
To:I(time - tpand)       0.499 -0.595 -0.184  0.045    -0.025    -0.143   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.039   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.4251626 -0.7215955  0.1398774  0.6145224  2.0300394 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Anxiety and Mood Disorders Level change 1.106 (1.043 to 1.174), p = 0.003
Norway Anxiety and Mood Disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.4467
s.e.  0.1845

sigma^2 = 0.9255:  log likelihood = -49.29
AIC=102.59   AICc=102.95   BIC=105.75
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 8.199699e-08 (Intr)
time        1.142621e-09 0     
Residual    1.603203e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.040261200 -0.315496102 -0.191533122 -0.567805396 -0.536486981 -0.304504715 
        Phi7         Phi8 
-0.007125384 -0.330958803 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -4.386350 0.011998102 28 -365.5870  0.0000
time                     0.020555 0.001293672 28   15.8886  0.0000
To                       0.862270 0.012148054 28   70.9801  0.0000
harmonic(month, 2, 12)1 -0.019470 0.007346871 28   -2.6501  0.0131
harmonic(month, 2, 12)2  0.014371 0.007697487 28    1.8670  0.0724
harmonic(month, 2, 12)3 -0.038992 0.008163618 28   -4.7763  0.0001
harmonic(month, 2, 12)4  0.012929 0.007501808 28    1.7234  0.0958
To:I(time - tpand)      -0.027869 0.001301932 28  -21.4055  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.962                                            
To                       0.721 -0.848                                     
harmonic(month, 2, 12)1  0.067 -0.031 -0.017                              
harmonic(month, 2, 12)2 -0.037  0.009  0.000 -0.143                       
harmonic(month, 2, 12)3  0.330 -0.320  0.160 -0.001     0.062             
harmonic(month, 2, 12)4 -0.036  0.031  0.013  0.039    -0.040    -0.135   
To:I(time - tpand)       0.920 -0.937  0.647  0.054     0.015     0.402   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.071   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.90576357 -0.32001339  0.08284602  0.63734932  1.68329103 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Anxiety and Mood Disorders Level change 2.369 (2.317 to 2.421), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.973 (0.97 to 0.975), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.106 (1.043 to 1.174), p = 0.003
Norway Anxiety and Mood Disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.02
AIC=140.05   AICc=140.13   BIC=141.92
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.816090e-07 (Intr)
time        6.240325e-09 0     
Residual    1.297533e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.001913602  0.015605855 -0.105562923  0.195323152 -0.217124136 -0.085549952 
        Phi7         Phi8         Phi9        Phi10        Phi11 
 0.121795462 -0.075811142 -0.018776102 -0.084025656 -0.406016658 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.918005 0.02670459 40 -221.61006  0.0000
time                     0.006558 0.00174084 40    3.76723  0.0005
To                       0.268912 0.04630317 40    5.80763  0.0000
harmonic(month, 2, 12)1 -0.018097 0.01159880 40   -1.56028  0.1266
harmonic(month, 2, 12)2  0.011017 0.01097290 40    1.00406  0.3214
harmonic(month, 2, 12)3  0.014137 0.01234431 40    1.14522  0.2589
harmonic(month, 2, 12)4 -0.017386 0.01079676 40   -1.61028  0.1152
To:I(time - tpand)       0.014797 0.00268404 40    5.51279  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.930                                            
To                       0.546 -0.730                                     
harmonic(month, 2, 12)1 -0.125  0.104 -0.079                              
harmonic(month, 2, 12)2 -0.011 -0.026  0.069  0.014                       
harmonic(month, 2, 12)3  0.175 -0.243  0.410 -0.064     0.068             
harmonic(month, 2, 12)4 -0.020  0.034 -0.015 -0.068    -0.049    -0.038   
To:I(time - tpand)       0.403 -0.356 -0.304  0.065    -0.015    -0.248   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0197683 -0.7108163 -0.1890457  0.6745163  2.5908460 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Anxiety and Mood Disorders Level change 1.309 (1.201 to 1.425), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.015 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.369 (2.317 to 2.421), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.973 (0.97 to 0.975), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.106 (1.043 to 1.174), p = 0.003
Norway Anxiety and Mood Disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.04:  log likelihood = -69.06
AIC=140.12   AICc=140.21   BIC=141.99
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.166741e-07 (Intr)
time        6.050360e-09 0     
Residual    2.654085e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.064913083  0.179053899  0.028747502  0.033770425  0.009799224 -0.611224188 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.3554689 0.015254590 40 -154.41050  0.0000
time                     0.0053116 0.000934854 40    5.68172  0.0000
To                       0.0632234 0.022699291 40    2.78526  0.0081
harmonic(month, 2, 12)1  0.0197997 0.018553170 40    1.06719  0.2923
harmonic(month, 2, 12)2 -0.0326646 0.004798943 40   -6.80662  0.0000
harmonic(month, 2, 12)3  0.0185238 0.018513949 40    1.00053  0.3231
harmonic(month, 2, 12)4  0.0080189 0.004682085 40    1.71268  0.0945
To:I(time - tpand)       0.0017940 0.001571156 40    1.14181  0.2603
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.430 -0.642                                     
harmonic(month, 2, 12)1 -0.114  0.078 -0.021                              
harmonic(month, 2, 12)2  0.045 -0.065  0.126 -0.077                       
harmonic(month, 2, 12)3 -0.066  0.008  0.117  0.039    -0.062             
harmonic(month, 2, 12)4  0.001  0.039 -0.059 -0.058     0.001    -0.026   
To:I(time - tpand)       0.493 -0.495 -0.239 -0.020    -0.002    -0.174   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.025   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.2734150 -0.6371616  0.0189445  0.6275732  2.1516448 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Anxiety and Mood Disorders Level change 1.065 (1.022 to 1.111), p = 0.008
Sweden Anxiety and Mood Disorders Trend change 1.002 (0.999 to 1.005), p = 0.26
Singapore Anxiety and Mood Disorders Level change 1.309 (1.201 to 1.425), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.015 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.369 (2.317 to 2.421), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.973 (0.97 to 0.975), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.106 (1.043 to 1.174), p = 0.003
Norway Anxiety and Mood Disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.057:  log likelihood = -69.43
AIC=140.86   AICc=140.95   BIC=142.73
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.975429e-07 (Intr)
time        4.862553e-09 0     
Residual    1.679667e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi 
-0.08886839 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.2050829 0.014445159 40 -152.65203  0.0000
time                     0.0037004 0.000911117 40    4.06141  0.0002
To                      -0.0150370 0.022655472 40   -0.66372  0.5107
harmonic(month, 2, 12)1  0.0397706 0.007649806 40    5.19891  0.0000
harmonic(month, 2, 12)2  0.0000498 0.007727479 40    0.00645  0.9949
harmonic(month, 2, 12)3 -0.0262171 0.007835760 40   -3.34583  0.0018
harmonic(month, 2, 12)4 -0.0085746 0.007788471 40   -1.10093  0.2775
To:I(time - tpand)       0.0023442 0.001627803 40    1.44008  0.1576
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.872                                            
To                       0.368 -0.603                                     
harmonic(month, 2, 12)1 -0.139  0.097 -0.054                              
harmonic(month, 2, 12)2 -0.031 -0.002  0.031  0.008                       
harmonic(month, 2, 12)3  0.114 -0.156  0.256 -0.028    -0.008             
harmonic(month, 2, 12)4  0.026 -0.003 -0.019 -0.004    -0.013     0.013   
To:I(time - tpand)       0.451 -0.517 -0.233  0.043     0.022    -0.089   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.006   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0712237 -0.5105202  0.0815082  0.6517687  2.2740489 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Anxiety and Mood Disorders Level change 0.985 (0.945 to 1.027), p = 0.511
USA Anxiety and Mood Disorders Trend change 1.002 (0.999 to 1.005), p = 0.158
Sweden Anxiety and Mood Disorders Level change 1.065 (1.022 to 1.111), p = 0.008
Sweden Anxiety and Mood Disorders Trend change 1.002 (0.999 to 1.005), p = 0.26
Singapore Anxiety and Mood Disorders Level change 1.309 (1.201 to 1.425), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.015 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.369 (2.317 to 2.421), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.973 (0.97 to 0.975), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.106 (1.043 to 1.174), p = 0.003
Norway Anxiety and Mood Disorders Trend change 0.99 (0.985 to 0.995), p < 0.001
China Anxiety and Mood Disorders Level change 2.635 (2.152 to 3.227), p < 0.001
China Anxiety and Mood Disorders Trend change 0.928 (0.911 to 0.946), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.283 (1.223 to 1.346), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.993 (0.99 to 0.996), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.097 (1.063 to 1.131), p < 0.001
Australia Anxiety and Mood Disorders Trend change 0.996 (0.994 to 0.998), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.231 (1.15 to 1.318), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.006 (1.001 to 1.011), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.231 (1.15 to 1.318), p < 0.001

Trend change

1.006 (1.001 to 1.011), p = 0.032

Australia

Level change

1.097 (1.063 to 1.131), p < 0.001

Trend change

0.996 (0.994 to 0.998), p < 0.001

Canada

Level change

1.283 (1.223 to 1.346), p < 0.001

Trend change

0.993 (0.99 to 0.996), p < 0.001

China

Level change

2.635 (2.152 to 3.227), p < 0.001

Trend change

0.928 (0.911 to 0.946), p < 0.001

Norway

Level change

1.106 (1.043 to 1.174), p = 0.003

Trend change

0.99 (0.985 to 0.995), p < 0.001

Peru

Level change

2.369 (2.317 to 2.421), p < 0.001

Trend change

0.973 (0.97 to 0.975), p < 0.001

Singapore

Level change

1.309 (1.201 to 1.425), p < 0.001

Trend change

1.015 (1.01 to 1.02), p < 0.001

Sweden

Level change

1.065 (1.022 to 1.111), p = 0.008

Trend change

1.002 (0.999 to 1.005), p = 0.26

USA

Level change

0.985 (0.945 to 1.027), p = 0.511

Trend change

1.002 (0.999 to 1.005), p = 0.158

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: ADHD and Eating Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.053:  log likelihood = -69.35
AIC=140.71   AICc=140.79   BIC=142.58
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.030284e-06 (Intr)
time        4.860784e-09 0     
Residual    1.136350e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5 
 0.2086722 -0.3016471  0.2464587 -0.2941351  0.2887645 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.732654 0.05065447 40 -132.91333  0.0000
time                    -0.012528 0.00317588 40   -3.94466  0.0003
To                      -0.609878 0.09397963 40   -6.48947  0.0000
harmonic(month, 2, 12)1 -0.009671 0.02475091 40   -0.39073  0.6981
harmonic(month, 2, 12)2  0.016647 0.03503865 40    0.47511  0.6373
harmonic(month, 2, 12)3  0.005227 0.02541504 40    0.20568  0.8381
harmonic(month, 2, 12)4  0.039373 0.03576907 40    1.10074  0.2776
To:I(time - tpand)       0.042875 0.00704214 40    6.08836  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.873                                            
To                       0.285 -0.447                                     
harmonic(month, 2, 12)1 -0.125  0.110 -0.069                              
harmonic(month, 2, 12)2 -0.006 -0.004 -0.003  0.026                       
harmonic(month, 2, 12)3  0.157 -0.179  0.246 -0.024     0.001             
harmonic(month, 2, 12)4  0.043 -0.031  0.025 -0.053    -0.004     0.059   
To:I(time - tpand)       0.383 -0.465 -0.461  0.049     0.044    -0.074   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.018   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.13503137 -0.73673819 -0.02335964  0.53907986  2.44884208 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.06:  log likelihood = -69.51
AIC=141.02   AICc=141.11   BIC=142.89
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.926940e-07 (Intr)
time        5.578233e-09 0     
Residual    9.885329e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.003395706 -0.143973483  0.116531888 -0.085661219 -0.154297294 -0.182782884 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -7.103822 0.03220364 40 -220.59068  0.0000
time                     0.005466 0.00196176 40    2.78608  0.0081
To                       0.167346 0.04203040 40    3.98154  0.0003
harmonic(month, 2, 12)1  0.011777 0.02130871 40    0.55267  0.5836
harmonic(month, 2, 12)2  0.056670 0.01340048 40    4.22892  0.0001
harmonic(month, 2, 12)3  0.006230 0.02196947 40    0.28358  0.7782
harmonic(month, 2, 12)4 -0.075719 0.01329962 40   -5.69333  0.0000
To:I(time - tpand)       0.025182 0.00271252 40    9.28376  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.906                                            
To                       0.483 -0.707                                     
harmonic(month, 2, 12)1 -0.091  0.079 -0.106                              
harmonic(month, 2, 12)2 -0.012 -0.018  0.054  0.033                       
harmonic(month, 2, 12)3  0.105 -0.107  0.163 -0.005     0.069             
harmonic(month, 2, 12)4  0.009  0.020 -0.017 -0.036     0.005    -0.002   
To:I(time - tpand)       0.576 -0.612 -0.013  0.103     0.016    -0.043   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.043   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7657617 -0.6750158 -0.0703690  0.3855582  2.5653491 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.062:  log likelihood = -69.55
AIC=141.1   AICc=141.18   BIC=142.97
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.150861e-07 (Intr)
time        2.997863e-09 0     
Residual    1.164268e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.231569970  0.090375265  0.003662638 -0.121718284 -0.672291908 -0.513239161 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.176022716 -0.038419639  0.066941390 -0.344043305 -0.343549326 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -5.696639 0.011395150 40 -499.9178  0.0000
time                     0.005198 0.000710662 40    7.3144  0.0000
To                       0.197386 0.017144556 40   11.5130  0.0000
harmonic(month, 2, 12)1  0.045546 0.030262861 40    1.5050  0.1402
harmonic(month, 2, 12)2 -0.025379 0.004927771 40   -5.1503  0.0000
harmonic(month, 2, 12)3  0.059021 0.029649979 40    1.9906  0.0534
harmonic(month, 2, 12)4 -0.026801 0.004841269 40   -5.5360  0.0000
To:I(time - tpand)       0.008191 0.001044768 40    7.8404  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.940                                            
To                       0.583 -0.749                                     
harmonic(month, 2, 12)1 -0.220  0.196 -0.143                              
harmonic(month, 2, 12)2  0.024 -0.041  0.077 -0.113                       
harmonic(month, 2, 12)3 -0.039  0.000  0.047  0.032    -0.017             
harmonic(month, 2, 12)4 -0.017  0.029 -0.007  0.013    -0.040    -0.084   
To:I(time - tpand)       0.419 -0.380 -0.248 -0.015    -0.008    -0.090   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.067   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7793011 -0.7656802 -0.1417781  0.5172337  2.6275920 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,0) with non-zero mean 

Coefficients:
          ar1      ar2     mean
      -0.3657  -0.4445  -0.1870
s.e.   0.1310   0.1279   0.0697

sigma^2 = 0.792:  log likelihood = -61.22
AIC=130.43   AICc=131.36   BIC=137.92
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 9.015598e-07 (Intr)
time        1.705028e-09 0     
Residual    1.048232e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
 0.2695646 -0.2593483 -0.9999999 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -8.487704 0.4851344 38 -17.495572  0.0000
time                     0.023079 0.0744303 38   0.310076  0.7582
To                       0.186994 0.9849948 38   0.189843  0.8504
harmonic(month, 2, 12)1  0.004892 0.3046220 38   0.016060  0.9873
harmonic(month, 2, 12)2  0.286436 0.2759767 38   1.037901  0.3059
harmonic(month, 2, 12)3 -0.565865 0.1586960 38  -3.565717  0.0010
harmonic(month, 2, 12)4 -0.389891 0.2670801 38  -1.459828  0.1526
China_Jan19             -1.126290 0.9378706 38  -1.200901  0.2372
China_Dec20             -1.530830 1.2765378 38  -1.199204  0.2379
To:I(time - tpand)      -0.002630 0.0624784 38  -0.042091  0.9666
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.992                                            
To                       0.951 -0.957                                     
harmonic(month, 2, 12)1 -0.891  0.908 -0.924                              
harmonic(month, 2, 12)2 -0.345  0.347 -0.353  0.469                       
harmonic(month, 2, 12)3  0.531 -0.464  0.433 -0.363     0.026             
harmonic(month, 2, 12)4  0.353 -0.292  0.259 -0.238    -0.048     0.420   
China_Jan19              0.979 -0.996  0.942 -0.902    -0.348     0.437   
China_Dec20              0.802 -0.819  0.931 -0.869    -0.341     0.310   
To:I(time - tpand)      -0.257  0.275 -0.525  0.460     0.192    -0.015   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.273                 
China_Dec20              0.168     0.808       
To:I(time - tpand)       0.048    -0.257 -0.768

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.3890640 -0.6751805 -0.3504980  0.5376205  2.8221449 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China ADHD and Eating Disorders Level change 1.206 (0.204 to 7.108), p = 0.85
China ADHD and Eating Disorders Trend change 0.997 (0.891 to 1.116), p = 0.967
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.3890
s.e.  0.1329

sigma^2 = 0.9632:  log likelihood = -66.79
AIC=137.57   AICc=137.84   BIC=141.32
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 9.099785e-07 (Intr)
time        9.178876e-09 0     
Residual    4.316675e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.1337902 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.435846 0.02876840 40 -188.95195  0.0000
time                     0.004187 0.00174814 40    2.39533  0.0214
To                       0.110820 0.03940946 40    2.81202  0.0076
harmonic(month, 2, 12)1 -0.010409 0.01376461 40   -0.75618  0.4540
harmonic(month, 2, 12)2 -0.023831 0.01294655 40   -1.84075  0.0731
harmonic(month, 2, 12)3  0.023835 0.01408893 40    1.69175  0.0985
harmonic(month, 2, 12)4 -0.004974 0.01264287 40   -0.39340  0.6961
To:I(time - tpand)       0.003114 0.00286257 40    1.08792  0.2831
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.874                                            
To                       0.357 -0.608                                     
harmonic(month, 2, 12)1 -0.099  0.063 -0.039                              
harmonic(month, 2, 12)2 -0.011 -0.001  0.057 -0.047                       
harmonic(month, 2, 12)3  0.052 -0.120  0.257  0.027     0.013             
harmonic(month, 2, 12)4  0.002  0.032 -0.046 -0.057     0.004    -0.051   
To:I(time - tpand)       0.522 -0.596 -0.132  0.064     0.000    -0.117   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.032   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.4727885 -0.5777157  0.3236088  0.6563528  1.4813946 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway ADHD and Eating Disorders Level change 1.117 (1.039 to 1.201), p = 0.008
Norway ADHD and Eating Disorders Trend change 1.003 (0.998 to 1.008), p = 0.283
China ADHD and Eating Disorders Level change 1.206 (0.204 to 7.108), p = 0.85
China ADHD and Eating Disorders Trend change 0.997 (0.891 to 1.116), p = 0.967
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.14:  log likelihood = -53.45
AIC=108.89   AICc=109.01   BIC=110.48
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 9.302764e-07 (Intr)
time        1.701648e-08 0     
Residual    6.580178e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.279078902 -0.009965086  0.535715939 -0.070960160 -0.017905881 -0.236486660 
        Phi7         Phi8 
-0.080990933 -0.307143196 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.930493 0.05637759 28 -122.92991  0.0000
time                    -0.009845 0.00603469 28   -1.63136  0.1140
To                       0.439439 0.06762007 28    6.49865  0.0000
harmonic(month, 2, 12)1  0.016384 0.01589170 28    1.03100  0.3114
harmonic(month, 2, 12)2  0.002968 0.00921602 28    0.32200  0.7498
harmonic(month, 2, 12)3 -0.006433 0.01723774 28   -0.37318  0.7118
harmonic(month, 2, 12)4  0.025206 0.00887970 28    2.83863  0.0083
To:I(time - tpand)       0.012483 0.00753452 28    1.65678  0.1087
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.928                                            
To                       0.363 -0.596                                     
harmonic(month, 2, 12)1  0.054 -0.134  0.291                              
harmonic(month, 2, 12)2  0.009 -0.099  0.219  0.171                       
harmonic(month, 2, 12)3 -0.168  0.037  0.351  0.081     0.119             
harmonic(month, 2, 12)4 -0.043  0.075 -0.093 -0.139    -0.036     0.049   
To:I(time - tpand)       0.853 -0.861  0.155  0.010     0.036    -0.269   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
          Min            Q1           Med            Q3           Max 
-1.3635284852 -0.5423390636  0.0008120699  0.7256630045  1.9996501451 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru ADHD and Eating Disorders Level change 1.552 (1.373 to 1.753), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.013 (0.999 to 1.026), p = 0.109
Norway ADHD and Eating Disorders Level change 1.117 (1.039 to 1.201), p = 0.008
Norway ADHD and Eating Disorders Trend change 1.003 (0.998 to 1.008), p = 0.283
China ADHD and Eating Disorders Level change 1.206 (0.204 to 7.108), p = 0.85
China ADHD and Eating Disorders Trend change 0.997 (0.891 to 1.116), p = 0.967
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: ADHD and Eating Disorders

Modeling ADHD and Eating Disorders for Singapore is not appropiate due to missing data in numerator.

Code
fake <- data.frame(x = seq(1, 48, 1), 
                   y = seq(1, 48, 1))

fake |> 
  ggplot(aes(x = x, y = y)) + 
  theme_void() + 
  annotate(geom = "text", 
           x = 24, 
           y = 24, 
           label = "Not calculable", 
           color = "black", 
           size = 6) -> plot7
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.058:  log likelihood = -69.46
AIC=140.91   AICc=141   BIC=142.78
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.206623e-06 (Intr)
time        4.169251e-09 0     
Residual    9.955434e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
         Phi1          Phi2          Phi3          Phi4          Phi5 
 0.0227672636  0.1103830095 -0.0712207714 -0.0038789337  0.0668937035 
         Phi6 
 0.0006282233 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.636977 0.06662554 40 -99.61611  0.0000
time                     0.027393 0.00374810 40   7.30864  0.0000
To                       0.155253 0.07494844 40   2.07147  0.0448
harmonic(month, 2, 12)1  0.029217 0.02491643 40   1.17261  0.2479
harmonic(month, 2, 12)2  0.004066 0.02594886 40   0.15670  0.8763
harmonic(month, 2, 12)3 -0.009684 0.02614512 40  -0.37040  0.7130
harmonic(month, 2, 12)4 -0.003127 0.02529639 40  -0.12361  0.9022
To:I(time - tpand)      -0.015819 0.00561907 40  -2.81527  0.0075
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.894                                            
To                       0.366 -0.605                                     
harmonic(month, 2, 12)1 -0.061  0.017 -0.013                              
harmonic(month, 2, 12)2  0.013 -0.032  0.074 -0.025                       
harmonic(month, 2, 12)3  0.081 -0.150  0.303  0.032     0.019             
harmonic(month, 2, 12)4  0.007  0.029 -0.038 -0.049     0.007    -0.037   
To:I(time - tpand)       0.586 -0.660 -0.051  0.091     0.029    -0.095   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.041   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.62368566 -0.58839218  0.08159998  0.53390529  2.35929661 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden ADHD and Eating Disorders Level change 1.168 (1.017 to 1.341), p = 0.045
Sweden ADHD and Eating Disorders Trend change 0.984 (0.974 to 0.995), p = 0.008
Peru ADHD and Eating Disorders Level change 1.552 (1.373 to 1.753), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.013 (0.999 to 1.026), p = 0.109
Norway ADHD and Eating Disorders Level change 1.117 (1.039 to 1.201), p = 0.008
Norway ADHD and Eating Disorders Trend change 1.003 (0.998 to 1.008), p = 0.283
China ADHD and Eating Disorders Level change 1.206 (0.204 to 7.108), p = 0.85
China ADHD and Eating Disorders Trend change 0.997 (0.891 to 1.116), p = 0.967
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.027:  log likelihood = -68.74
AIC=139.48   AICc=139.56   BIC=141.35
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.945466e-07 (Intr)
time        2.310128e-09 0     
Residual    1.172404e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.1612528 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.349074 0.06012418 40 -88.96709  0.0000
time                     0.005891 0.00374453 40   1.57332  0.1235
To                      -0.115033 0.09161005 40  -1.25568  0.2165
harmonic(month, 2, 12)1  0.028909 0.03091744 40   0.93505  0.3554
harmonic(month, 2, 12)2 -0.010696 0.02819870 40  -0.37931  0.7065
harmonic(month, 2, 12)3  0.006083 0.03117191 40   0.19515  0.8463
harmonic(month, 2, 12)4 -0.001786 0.02817465 40  -0.06340  0.9498
To:I(time - tpand)       0.013081 0.00665009 40   1.96698  0.0561
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.868                                            
To                       0.341 -0.579                                     
harmonic(month, 2, 12)1 -0.130  0.089 -0.045                              
harmonic(month, 2, 12)2 -0.032 -0.001  0.038 -0.012                       
harmonic(month, 2, 12)3  0.076 -0.130  0.249 -0.025    -0.001             
harmonic(month, 2, 12)4  0.005  0.019 -0.027 -0.023    -0.023    -0.026   
To:I(time - tpand)       0.473 -0.549 -0.228  0.041     0.015    -0.110   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.032   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.60039962 -0.92497281  0.01616029  0.69575076  2.06241036 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA ADHD and Eating Disorders Level change 0.891 (0.753 to 1.055), p = 0.217
USA ADHD and Eating Disorders Trend change 1.013 (1.001 to 1.026), p = 0.056
Sweden ADHD and Eating Disorders Level change 1.168 (1.017 to 1.341), p = 0.045
Sweden ADHD and Eating Disorders Trend change 0.984 (0.974 to 0.995), p = 0.008
Peru ADHD and Eating Disorders Level change 1.552 (1.373 to 1.753), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.013 (0.999 to 1.026), p = 0.109
Norway ADHD and Eating Disorders Level change 1.117 (1.039 to 1.201), p = 0.008
Norway ADHD and Eating Disorders Trend change 1.003 (0.998 to 1.008), p = 0.283
China ADHD and Eating Disorders Level change 1.206 (0.204 to 7.108), p = 0.85
China ADHD and Eating Disorders Trend change 0.997 (0.891 to 1.116), p = 0.967
Canada ADHD and Eating Disorders Level change 1.218 (1.18 to 1.257), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.008 (1.006 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.182 (1.094 to 1.277), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.026 (1.02 to 1.031), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.543 (0.457 to 0.646), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.044 (1.03 to 1.057), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

0.543 (0.457 to 0.646), p < 0.001

Trend change

1.044 (1.03 to 1.057), p < 0.001

Australia

Level change

1.182 (1.094 to 1.277), p < 0.001

Trend change

1.026 (1.02 to 1.031), p < 0.001

Canada

Level change

1.218 (1.18 to 1.257), p < 0.001

Trend change

1.008 (1.006 to 1.01), p < 0.001

China

Level change

1.206 (0.204 to 7.108), p = 0.85

Trend change

0.997 (0.891 to 1.116), p = 0.967

Norway

Level change

1.117 (1.039 to 1.201), p = 0.008

Trend change

1.003 (0.998 to 1.008), p = 0.283

Peru

Level change

1.552 (1.373 to 1.753), p < 0.001

Trend change

1.013 (0.999 to 1.026), p = 0.109

Sweden

Level change

1.168 (1.017 to 1.341), p = 0.045

Trend change

0.984 (0.974 to 0.995), p = 0.008

USA

Level change

0.891 (0.753 to 1.055), p = 0.217

Trend change

1.013 (1.001 to 1.026), p = 0.056

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Bipolar, Schizophrenia and other Psycotic Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.05:  log likelihood = -69.28
AIC=140.57   AICc=140.65   BIC=142.44
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.115527e-06 (Intr)
time        7.073196e-09 0     
Residual    1.613305e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.30559085  0.15212461 -0.03866796  0.07274164 -0.16423940 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.857159 0.06059601 40 -96.65916  0.0000
time                     0.000564 0.00368181 40   0.15313  0.8791
To                      -0.026229 0.08977771 40  -0.29215  0.7717
harmonic(month, 2, 12)1 -0.046368 0.03434291 40  -1.35014  0.1846
harmonic(month, 2, 12)2  0.033049 0.01894657 40   1.74433  0.0888
harmonic(month, 2, 12)3  0.050115 0.03400383 40   1.47379  0.1484
harmonic(month, 2, 12)4  0.047282 0.01909303 40   2.47642  0.0176
To:I(time - tpand)      -0.009604 0.00714618 40  -1.34400  0.1865
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.891                                            
To                       0.331 -0.526                                     
harmonic(month, 2, 12)1 -0.075  0.070 -0.024                              
harmonic(month, 2, 12)2  0.003 -0.035  0.106  0.018                       
harmonic(month, 2, 12)3  0.064 -0.096  0.201 -0.012     0.035             
harmonic(month, 2, 12)4 -0.019  0.038 -0.053 -0.091    -0.030     0.006   
To:I(time - tpand)       0.472 -0.534 -0.297  0.018    -0.022    -0.099   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.029   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.17922288 -0.50827685 -0.01805037  0.58991116  2.43055895 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.055:  log likelihood = -69.4
AIC=140.79   AICc=140.88   BIC=142.66
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.860832e-07 (Intr)
time        8.155855e-09 0     
Residual    1.231073e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
-0.2078001 -0.2798133 -0.2812477 -0.1378737 -0.3505320 -0.1518825 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -6.227006 0.016763686 40 -371.4580  0.0000
time                    -0.009128 0.001054210 40   -8.6588  0.0000
To                       0.175503 0.024864598 40    7.0584  0.0000
harmonic(month, 2, 12)1  0.002770 0.013127300 40    0.2110  0.8340
harmonic(month, 2, 12)2  0.055403 0.017059060 40    3.2477  0.0024
harmonic(month, 2, 12)3  0.104863 0.013863517 40    7.5640  0.0000
harmonic(month, 2, 12)4 -0.015657 0.017228631 40   -0.9088  0.3689
To:I(time - tpand)       0.009298 0.001663055 40    5.5908  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.908                                            
To                       0.487 -0.696                                     
harmonic(month, 2, 12)1 -0.065  0.059 -0.082                              
harmonic(month, 2, 12)2 -0.057  0.038 -0.006 -0.067                       
harmonic(month, 2, 12)3  0.177 -0.186  0.165  0.006     0.010             
harmonic(month, 2, 12)4  0.019 -0.005  0.003  0.016     0.003    -0.051   
To:I(time - tpand)       0.477 -0.492 -0.166  0.089    -0.004     0.070   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.019   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.90709615 -0.71018944 -0.02973146  0.64113090  2.58399485 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.14:  log likelihood = -71.25
AIC=144.5   AICc=144.59   BIC=146.37
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.567990e-07 (Intr)
time        2.782674e-09 0     
Residual    1.213303e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.052211983 -0.051530676 -0.016163886 -0.009680077 -0.221763710 -0.210492180 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.246061227  0.016816994 -0.043892718 -0.103230614 -0.359031530 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -4.872828 0.014390263 40 -338.6198  0.0000
time                    -0.003187 0.000940753 40   -3.3881  0.0016
To                       0.245772 0.025521703 40    9.6299  0.0000
harmonic(month, 2, 12)1  0.039214 0.013495466 40    2.9057  0.0059
harmonic(month, 2, 12)2 -0.009980 0.006875658 40   -1.4515  0.1544
harmonic(month, 2, 12)3 -0.012589 0.013327109 40   -0.9446  0.3505
harmonic(month, 2, 12)4 -0.012443 0.006856174 40   -1.8149  0.0770
To:I(time - tpand)      -0.010327 0.001578799 40   -6.5407  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.934                                            
To                       0.571 -0.743                                     
harmonic(month, 2, 12)1 -0.197  0.168 -0.119                              
harmonic(month, 2, 12)2 -0.021 -0.004  0.053  0.041                       
harmonic(month, 2, 12)3  0.064 -0.095  0.218 -0.025    -0.014             
harmonic(month, 2, 12)4 -0.011  0.024 -0.020 -0.014    -0.037     0.028   
To:I(time - tpand)       0.313 -0.260 -0.374  0.028    -0.021    -0.212   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.034   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.86936210 -0.67055189 -0.07947393  0.56295858  3.55526027 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,2) with zero mean 

Coefficients:
         ar1      ar2      ma1     ma2
      1.4173  -0.9643  -1.2135  0.8534
s.e.  0.0627   0.0488   0.1335  0.1016

sigma^2 = 0.7817:  log likelihood = -61.22
AIC=132.44   AICc=133.87   BIC=141.8
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.762363e-05 (Intr)
time        2.200734e-08 0     
Residual    1.467441e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.5764122  0.4139263  0.9717206 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -8.342834 0.5891538 38 -14.160708  0.0000
time                     0.061285 0.0543285 38   1.128045  0.2664
To                       0.187267 0.5699251 38   0.328582  0.7443
harmonic(month, 2, 12)1  0.198975 0.2139794 38   0.929878  0.3583
harmonic(month, 2, 12)2 -0.196471 0.1393599 38  -1.409812  0.1667
harmonic(month, 2, 12)3 -0.243488 0.1734380 38  -1.403893  0.1685
harmonic(month, 2, 12)4  0.027033 0.1317011 38   0.205260  0.8385
China_Jan19              0.489169 0.7775969 38   0.629078  0.5331
China_Dec20              0.118473 0.5975379 38   0.198269  0.8439
To:I(time - tpand)      -0.111764 0.0691309 38  -1.616709  0.1142
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.712                                            
To                       0.481 -0.699                                     
harmonic(month, 2, 12)1 -0.224  0.330 -0.380                              
harmonic(month, 2, 12)2 -0.042  0.144 -0.122  0.284                       
harmonic(month, 2, 12)3  0.202 -0.261  0.309 -0.024    -0.089             
harmonic(month, 2, 12)4  0.105 -0.183  0.196  0.033    -0.016     0.120   
China_Jan19              0.215 -0.806  0.471 -0.299    -0.155     0.196   
China_Dec20              0.089 -0.144  0.279 -0.505    -0.277    -0.013   
To:I(time - tpand)       0.491 -0.682  0.173  0.108     0.059     0.139   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.137                 
China_Dec20             -0.033     0.140       
To:I(time - tpand)       0.128     0.529 -0.509

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0108361 -0.5720898 -0.1463266  0.6565968  2.8414034 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.09:  log likelihood = -70.17
AIC=142.34   AICc=142.43   BIC=144.22
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 8.563293e-07 (Intr)
time        7.582154e-09 0     
Residual    3.130716e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
     Phi 
0.332498 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -5.084560 0.021215347 40 -239.66423  0.0000
time                    -0.000436 0.001295206 40   -0.33641  0.7383
To                       0.140654 0.029287821 40    4.80246  0.0000
harmonic(month, 2, 12)1 -0.024899 0.010534431 40   -2.36357  0.0230
harmonic(month, 2, 12)2  0.000776 0.008732275 40    0.08891  0.9296
harmonic(month, 2, 12)3 -0.017982 0.010658229 40   -1.68718  0.0994
harmonic(month, 2, 12)4  0.011159 0.008554733 40    1.30443  0.1995
To:I(time - tpand)      -0.012970 0.002344302 40   -5.53244  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.865                                            
To                       0.317 -0.565                                     
harmonic(month, 2, 12)1 -0.105  0.070 -0.025                              
harmonic(month, 2, 12)2 -0.025  0.006  0.077 -0.026                       
harmonic(month, 2, 12)3  0.029 -0.089  0.248  0.011    -0.003             
harmonic(month, 2, 12)4 -0.021  0.051 -0.066 -0.049    -0.008    -0.029   
To:I(time - tpand)       0.495 -0.587 -0.173  0.041    -0.027    -0.140   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.035   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.3297074 -0.5292972  0.1814659  0.5496486  2.6885586 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.151 (1.09 to 1.215), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.987 (0.983 to 0.991), p < 0.001
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.6861
s.e.  0.1269

sigma^2 = 0.5996:  log likelihood = -41.69
AIC=87.37   AICc=87.74   BIC=90.54
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.701158e-07 (Intr)
time        5.173771e-09 0     
Residual    1.087706e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.05275314 -0.06674591  0.25819642  0.21504404  0.24876960 -0.47642344 
       Phi7        Phi8 
-0.34253943 -0.44335494 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -6.038378 0.023360971 28 -258.48145  0.0000
time                     0.017219 0.002535756 28    6.79029  0.0000
To                       0.909534 0.024833356 28   36.62548  0.0000
harmonic(month, 2, 12)1  0.001731 0.006237965 28    0.27748  0.7835
harmonic(month, 2, 12)2 -0.000781 0.003383320 28   -0.23073  0.8192
harmonic(month, 2, 12)3 -0.066316 0.006848736 28   -9.68294  0.0000
harmonic(month, 2, 12)4  0.019629 0.003322768 28    5.90750  0.0000
To:I(time - tpand)      -0.012276 0.002620423 28   -4.68470  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.954                                            
To                       0.635 -0.802                                     
harmonic(month, 2, 12)1  0.011 -0.038  0.103                              
harmonic(month, 2, 12)2  0.102 -0.147  0.174  0.192                       
harmonic(month, 2, 12)3 -0.013 -0.073  0.315  0.004     0.081             
harmonic(month, 2, 12)4 -0.020  0.050 -0.100 -0.099     0.011     0.063   
To:I(time - tpand)       0.919 -0.927  0.555  0.009     0.138    -0.107   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.6045345 -0.5761636 -0.1118516  1.1086781  1.7005916 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.151 (1.09 to 1.215), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.987 (0.983 to 0.991), p < 0.001
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
Error in lme.formula(fixed = zz ~ time + To + harmonic(month, 2, 12) + : nlminb problem, convergence error code = 1
  message = iteration limit reached without convergence (10)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.6861
s.e.  0.1269

sigma^2 = 0.5996:  log likelihood = -41.69
AIC=87.37   AICc=87.74   BIC=90.54
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.701158e-07 (Intr)
time        5.173771e-09 0     
Residual    1.087706e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.05275314 -0.06674591  0.25819642  0.21504404  0.24876960 -0.47642344 
       Phi7        Phi8 
-0.34253943 -0.44335494 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -6.038378 0.023360971 28 -258.48145  0.0000
time                     0.017219 0.002535756 28    6.79029  0.0000
To                       0.909534 0.024833356 28   36.62548  0.0000
harmonic(month, 2, 12)1  0.001731 0.006237965 28    0.27748  0.7835
harmonic(month, 2, 12)2 -0.000781 0.003383320 28   -0.23073  0.8192
harmonic(month, 2, 12)3 -0.066316 0.006848736 28   -9.68294  0.0000
harmonic(month, 2, 12)4  0.019629 0.003322768 28    5.90750  0.0000
To:I(time - tpand)      -0.012276 0.002620423 28   -4.68470  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.954                                            
To                       0.635 -0.802                                     
harmonic(month, 2, 12)1  0.011 -0.038  0.103                              
harmonic(month, 2, 12)2  0.102 -0.147  0.174  0.192                       
harmonic(month, 2, 12)3 -0.013 -0.073  0.315  0.004     0.081             
harmonic(month, 2, 12)4 -0.020  0.050 -0.100 -0.099     0.011     0.063   
To:I(time - tpand)       0.919 -0.927  0.555  0.009     0.138    -0.107   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.6045345 -0.5761636 -0.1118516  1.1086781  1.7005916 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.151 (1.09 to 1.215), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.987 (0.983 to 0.991), p < 0.001
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,1) with zero mean 

Coefficients:
          ar1     ma1
      -0.8901  0.6781
s.e.   0.0931  0.1454

sigma^2 = 0.9133:  log likelihood = -65.08
AIC=136.16   AICc=136.71   BIC=141.77
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.422801e-07 (Intr)
time        1.929600e-09 0     
Residual    9.396682e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.24172660  0.01165474 -0.21396291  0.10942432 -0.07856218 -0.02985355 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -7.270242 0.05973683 40 -121.70451  0.0000
time                    -0.007007 0.00377229 40   -1.85740  0.0706
To                       0.376296 0.09182771 40    4.09785  0.0002
harmonic(month, 2, 12)1  0.080200 0.03356108 40    2.38967  0.0217
harmonic(month, 2, 12)2  0.056996 0.03828835 40    1.48860  0.1444
harmonic(month, 2, 12)3 -0.021115 0.03628684 40   -0.58188  0.5639
harmonic(month, 2, 12)4 -0.054218 0.03821502 40   -1.41875  0.1637
To:I(time - tpand)       0.004866 0.00627140 40    0.77589  0.4424
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.879                                            
To                       0.412 -0.653                                     
harmonic(month, 2, 12)1 -0.150  0.097 -0.075                              
harmonic(month, 2, 12)2 -0.030  0.011  0.019 -0.023                       
harmonic(month, 2, 12)3  0.113 -0.159  0.246  0.001    -0.052             
harmonic(month, 2, 12)4  0.061 -0.015 -0.013  0.013     0.015     0.006   
To:I(time - tpand)       0.461 -0.512 -0.184  0.064     0.024    -0.074   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.006   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.26471208 -0.73772384  0.01977432  0.76879113  3.31496792 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.457 (1.23 to 1.726), p < 0.001
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.005 (0.993 to 1.017), p = 0.442
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.151 (1.09 to 1.215), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.987 (0.983 to 0.991), p < 0.001
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.034:  log likelihood = -68.92
AIC=139.83   AICc=139.92   BIC=141.7
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 8.023230e-07 (Intr)
time        4.450071e-09 0     
Residual    1.295512e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
0.01478648 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -4.911701 0.04715513 40 -104.16047  0.0000
time                     0.005274 0.00295463 40    1.78507  0.0818
To                      -0.170193 0.07610043 40   -2.23643  0.0310
harmonic(month, 2, 12)1  0.026375 0.02584751 40    1.02042  0.3137
harmonic(month, 2, 12)2  0.025584 0.02500457 40    1.02318  0.3124
harmonic(month, 2, 12)3 -0.018818 0.02589919 40   -0.72660  0.4717
harmonic(month, 2, 12)4  0.037928 0.02519107 40    1.50561  0.1400
To:I(time - tpand)      -0.002797 0.00561238 40   -0.49842  0.6209
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.871                                            
To                       0.346 -0.572                                     
harmonic(month, 2, 12)1 -0.137  0.098 -0.049                              
harmonic(month, 2, 12)2 -0.042  0.001  0.032 -0.004                       
harmonic(month, 2, 12)3  0.108 -0.155  0.255 -0.046    -0.015             
harmonic(month, 2, 12)4  0.006  0.006 -0.024 -0.004    -0.018     0.002   
To:I(time - tpand)       0.429 -0.493 -0.291  0.038     0.017    -0.095   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.011   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.3096678 -0.6726997 -0.1992589  0.8695448  2.0220097 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.844 (0.733 to 0.971), p = 0.031
USA Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.997 (0.987 to 1.008), p = 0.621
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.457 (1.23 to 1.726), p < 0.001
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.005 (0.993 to 1.017), p = 0.442
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.483 (2.374 to 2.597), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.988 (0.983 to 0.992), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.151 (1.09 to 1.215), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.987 (0.983 to 0.991), p < 0.001
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.206 (0.432 to 3.366), p = 0.744
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.894 (0.79 to 1.013), p = 0.114
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.279 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.987 to 0.993), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.192 (1.138 to 1.248), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.009 (1.006 to 1.012), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.974 (0.825 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.977 to 1.004), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

0.974 (0.825 to 1.15), p = 0.772

Trend change

0.99 (0.977 to 1.004), p = 0.187

Australia

Level change

1.192 (1.138 to 1.248), p < 0.001

Trend change

1.009 (1.006 to 1.012), p < 0.001

Canada

Level change

1.279 (1.22 to 1.34), p < 0.001

Trend change

0.99 (0.987 to 0.993), p < 0.001

China

Level change

1.206 (0.432 to 3.366), p = 0.744

Trend change

0.894 (0.79 to 1.013), p = 0.114

Norway

Level change

1.151 (1.09 to 1.215), p < 0.001

Trend change

0.987 (0.983 to 0.991), p < 0.001

Peru

Level change

2.483 (2.374 to 2.597), p < 0.001

Trend change

0.988 (0.983 to 0.992), p < 0.001

Singapore

Level change

2.483 (2.374 to 2.597), p < 0.001

Trend change

0.988 (0.983 to 0.992), p < 0.001

Sweden

Level change

1.457 (1.23 to 1.726), p < 0.001

Trend change

1.005 (0.993 to 1.017), p = 0.442

USA

Level change

0.844 (0.733 to 0.971), p = 0.031

Trend change

0.997 (0.987 to 1.008), p = 0.621

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Dementia

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.044:  log likelihood = -69.15
AIC=140.29   AICc=140.38   BIC=142.16
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.217327e-06 (Intr)
time        3.422500e-09 0     
Residual    9.504926e-01       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5 
 0.079858823 -0.027478945 -0.036011936  0.193426314  0.003653354 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -9.770843 0.19976572 40 -48.91151  0.0000
time                     0.017341 0.01131693 40   1.53228  0.1333
To                       0.173113 0.23750621 40   0.72888  0.4703
harmonic(month, 2, 12)1 -0.166741 0.07449669 40  -2.23823  0.0308
harmonic(month, 2, 12)2  0.190630 0.07305487 40   2.60941  0.0127
harmonic(month, 2, 12)3  0.204402 0.07282634 40   2.80670  0.0077
harmonic(month, 2, 12)4  0.119413 0.07399398 40   1.61382  0.1144
To:I(time - tpand)      -0.022431 0.01871362 40  -1.19864  0.2377
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.894                                            
To                       0.332 -0.546                                     
harmonic(month, 2, 12)1 -0.007  0.033 -0.010                              
harmonic(month, 2, 12)2 -0.032 -0.034  0.066 -0.044                       
harmonic(month, 2, 12)3  0.078 -0.167  0.310 -0.081     0.129             
harmonic(month, 2, 12)4  0.005 -0.010 -0.021 -0.155    -0.004    -0.031   
To:I(time - tpand)       0.552 -0.625 -0.165  0.051     0.031    -0.077   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.001   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.03752800 -0.65682799  0.05414143  0.62092604  2.38632745 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.071:  log likelihood = -69.75
AIC=141.5   AICc=141.59   BIC=143.38
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.704559e-07 (Intr)
time        2.503493e-09 0     
Residual    1.093167e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
-0.4274509 -0.1682413 -0.1057179 -0.4868659 -0.3008830 -0.3109109 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -7.541000 0.02267641 40 -332.5483  0.0000
time                    -0.010959 0.00142880 40   -7.6702  0.0000
To                       0.640115 0.03187098 40   20.0846  0.0000
harmonic(month, 2, 12)1 -0.004788 0.01932944 40   -0.2477  0.8056
harmonic(month, 2, 12)2  0.056615 0.01919910 40    2.9488  0.0053
harmonic(month, 2, 12)3  0.145151 0.02029223 40    7.1530  0.0000
harmonic(month, 2, 12)4 -0.019122 0.01944548 40   -0.9834  0.3313
To:I(time - tpand)      -0.016649 0.00214148 40   -7.7745  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.513 -0.730                                     
harmonic(month, 2, 12)1 -0.036  0.030 -0.032                              
harmonic(month, 2, 12)2 -0.065  0.047  0.010 -0.068                       
harmonic(month, 2, 12)3  0.150 -0.156  0.159  0.014     0.001             
harmonic(month, 2, 12)4  0.014 -0.001 -0.007  0.014    -0.007    -0.059   
To:I(time - tpand)       0.506 -0.519 -0.083  0.056    -0.043     0.041   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.014   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.5983404 -0.7855670  0.1079683  0.7721604  2.5456289 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.04:  log likelihood = -69.06
AIC=140.11   AICc=140.2   BIC=141.98
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.462084e-07 (Intr)
time        2.410179e-09 0     
Residual    1.234109e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.42762688 -0.22813088 -0.36786903 -0.63137236 -0.57470945 -0.14040098 
       Phi7        Phi8        Phi9       Phi10       Phi11 
-0.16444596 -0.42194315 -0.41146901  0.09670013 -0.08095897 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -5.748068 0.008654960 40 -664.1358  0.0000
time                     0.002535 0.000541615 40    4.6800  0.0000
To                       0.376361 0.012346552 40   30.4830  0.0000
harmonic(month, 2, 12)1 -0.013784 0.012955295 40   -1.0640  0.2937
harmonic(month, 2, 12)2  0.004447 0.024306807 40    0.1830  0.8558
harmonic(month, 2, 12)3 -0.016437 0.013334124 40   -1.2327  0.2249
harmonic(month, 2, 12)4  0.023569 0.024129233 40    0.9768  0.3346
To:I(time - tpand)      -0.000096 0.000792550 40   -0.1215  0.9039
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.928                                            
To                       0.551 -0.739                                     
harmonic(month, 2, 12)1 -0.017  0.029 -0.024                              
harmonic(month, 2, 12)2 -0.014  0.044 -0.076 -0.057                       
harmonic(month, 2, 12)3  0.136 -0.102  0.024  0.024     0.037             
harmonic(month, 2, 12)4  0.026 -0.051  0.041  0.015    -0.002     0.005   
To:I(time - tpand)       0.496 -0.476 -0.145  0.019     0.063     0.148   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.031   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.13782001 -0.57595325  0.03470004  0.63266532  2.29286003 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,1) with zero mean 

Coefficients:
          ar1      ar2     ma1
      -0.7437  -0.4222  0.7030
s.e.   0.1689   0.1312  0.1457

sigma^2 = 0.8602:  log likelihood = -63.27
AIC=134.53   AICc=135.46   BIC=142.02
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.028735e-05 (Intr)
time        5.909666e-09 0     
Residual    8.784844e-01       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.7689804 -0.4530664  0.8000934 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -9.851531 0.5057097 38 -19.480605  0.0000
time                    -0.033246 0.0581403 38  -0.571832  0.5708
To                       0.763854 0.5839129 38   1.308163  0.1987
harmonic(month, 2, 12)1  0.058460 0.1965069 38   0.297498  0.7677
harmonic(month, 2, 12)2 -0.247809 0.1643531 38  -1.507784  0.1399
harmonic(month, 2, 12)3 -0.017844 0.1221990 38  -0.146027  0.8847
harmonic(month, 2, 12)4  0.140542 0.1454695 38   0.966126  0.3401
China_Jan19              1.563625 0.8305384 38   1.882665  0.0674
China_Dec20              0.262654 0.5335048 38   0.492319  0.6253
To:I(time - tpand)       0.053197 0.0580337 38   0.916660  0.3651
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.717                                            
To                       0.601 -0.829                                     
harmonic(month, 2, 12)1 -0.356  0.462 -0.489                              
harmonic(month, 2, 12)2 -0.165  0.250 -0.258  0.356                       
harmonic(month, 2, 12)3  0.104 -0.127  0.162  0.013    -0.017             
harmonic(month, 2, 12)4  0.074 -0.116  0.110 -0.029    -0.022    -0.036   
China_Jan19              0.328 -0.871  0.636 -0.407    -0.224     0.101   
China_Dec20              0.251 -0.331  0.378 -0.679    -0.405     0.044   
To:I(time - tpand)       0.523 -0.744  0.433  0.042     0.045     0.053   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.093                 
China_Dec20              0.004     0.293       
To:I(time - tpand)       0.097     0.646 -0.316

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9459507 -0.6874835 -0.2062734  0.7567901  2.8753623 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.883:  log likelihood = -83.3
AIC=168.59   AICc=168.68   BIC=170.47
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.955435e-07 (Intr)
time        8.458759e-09 0     
Residual    2.931772e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
-0.1096549 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.198072 0.02354609 40 -263.23146  0.0000
time                     0.000491 0.00146191 40    0.33576  0.7388
To                       0.029041 0.03494025 40    0.83117  0.4108
harmonic(month, 2, 12)1 -0.046040 0.01211751 40   -3.79947  0.0005
harmonic(month, 2, 12)2 -0.000075 0.01241626 40   -0.00602  0.9952
harmonic(month, 2, 12)3 -0.010222 0.01238008 40   -0.82570  0.4139
harmonic(month, 2, 12)4  0.025001 0.01224575 40    2.04161  0.0478
To:I(time - tpand)      -0.003338 0.00251069 40   -1.32963  0.1912
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.877                                            
To                       0.375 -0.613                                     
harmonic(month, 2, 12)1 -0.104  0.079 -0.052                              
harmonic(month, 2, 12)2 -0.015  0.002  0.044 -0.034                       
harmonic(month, 2, 12)3  0.090 -0.149  0.263  0.013     0.016             
harmonic(month, 2, 12)4  0.014  0.011 -0.035 -0.054     0.013    -0.013   
To:I(time - tpand)       0.478 -0.538 -0.194  0.060     0.007    -0.098   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.008   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.9464732 -0.3349271  0.2471336  0.6346616  1.1754075 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Dementia Level change 1.029 (0.965 to 1.098), p = 0.411
Norway Dementia Trend change 0.997 (0.992 to 1.001), p = 0.191
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.117:  log likelihood = -53.07
AIC=108.15   AICc=108.27   BIC=109.73
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.512150e-07 (Intr)
time        2.463220e-09 0     
Residual    1.959461e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6       Phi7 
 0.1506030 -0.1937753 -0.2114405  0.2189966 -0.2790947 -0.2905489 -0.2027746 
      Phi8 
-0.3390503 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -8.769438 0.03245071 28 -270.23872  0.0000
time                     0.006112 0.00367790 28    1.66179  0.1077
To                       0.520015 0.03853794 28   13.49357  0.0000
harmonic(month, 2, 12)1 -0.056359 0.03420331 28   -1.64775  0.1106
harmonic(month, 2, 12)2  0.039345 0.01099328 28    3.57904  0.0013
harmonic(month, 2, 12)3 -0.013766 0.03219115 28   -0.42763  0.6722
harmonic(month, 2, 12)4  0.008658 0.01075788 28    0.80479  0.4277
To:I(time - tpand)       0.016125 0.00348782 28    4.62309  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.961                                            
To                       0.740 -0.862                                     
harmonic(month, 2, 12)1 -0.160  0.200 -0.215                              
harmonic(month, 2, 12)2 -0.033  0.007 -0.022  0.053                       
harmonic(month, 2, 12)3 -0.009 -0.025  0.030  0.006     0.198             
harmonic(month, 2, 12)4  0.080 -0.072  0.053 -0.178     0.017     0.000   
To:I(time - tpand)       0.910 -0.925  0.637 -0.126     0.042     0.013   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.056   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7400470 -0.7185540 -0.2856532  0.8429810  2.3533158 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Dementia Level change 1.682 (1.569 to 1.803), p < 0.001
Peru Dementia Trend change 1.016 (1.01 to 1.023), p < 0.001
Norway Dementia Level change 1.029 (0.965 to 1.098), p = 0.411
Norway Dementia Trend change 0.997 (0.992 to 1.001), p = 0.191
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.035:  log likelihood = -68.95
AIC=139.89   AICc=139.98   BIC=141.76
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.108928e-06 (Intr)
time        3.238175e-09 0     
Residual    1.235787e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.167412767  0.022542593  0.477223450  0.542506875  0.399836676 -0.109792686 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.381923163 -0.364017679 -0.001084507 -0.088470590  0.001789914 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -7.731715 0.09860260 40 -78.41289  0.0000
time                     0.008864 0.00569059 40   1.55766  0.1272
To                       0.302950 0.08798190 40   3.44332  0.0014
harmonic(month, 2, 12)1 -0.050538 0.01433599 40  -3.52526  0.0011
harmonic(month, 2, 12)2 -0.003959 0.01239048 40  -0.31955  0.7510
harmonic(month, 2, 12)3  0.058107 0.01656105 40   3.50867  0.0011
harmonic(month, 2, 12)4  0.021055 0.01225698 40   1.71779  0.0936
To:I(time - tpand)      -0.005785 0.01205794 40  -0.47978  0.6340
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.913                                            
To                       0.117 -0.282                                     
harmonic(month, 2, 12)1 -0.017 -0.003  0.157                              
harmonic(month, 2, 12)2  0.018 -0.073  0.235  0.117                       
harmonic(month, 2, 12)3 -0.118  0.040  0.518  0.045     0.165             
harmonic(month, 2, 12)4  0.036 -0.018 -0.096 -0.080    -0.023    -0.014   
To:I(time - tpand)       0.687 -0.789 -0.216 -0.037    -0.011    -0.341   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.064   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.12026181 -0.75666434 -0.06252793  0.77418090  2.23841353 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Dementia Level change 1.354 (1.151 to 1.592), p = 0.001
Singapore Dementia Trend change 0.994 (0.972 to 1.017), p = 0.634
Peru Dementia Level change 1.682 (1.569 to 1.803), p < 0.001
Peru Dementia Trend change 1.016 (1.01 to 1.023), p < 0.001
Norway Dementia Level change 1.029 (0.965 to 1.098), p = 0.411
Norway Dementia Trend change 0.997 (0.992 to 1.001), p = 0.191
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.05:  log likelihood = -69.29
AIC=140.58   AICc=140.67   BIC=142.45
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.076495e-07 (Intr)
time        1.323334e-09 0     
Residual    1.174546e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.035831545 -0.003135299  0.422780282 -0.011122587 -0.034504578 -0.442731003 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.667525 0.07340919 40 -90.82684  0.0000
time                    -0.001755 0.00456692 40  -0.38418  0.7029
To                       0.044734 0.11152161 40   0.40112  0.6905
harmonic(month, 2, 12)1 -0.009187 0.05182538 40  -0.17727  0.8602
harmonic(month, 2, 12)2 -0.064005 0.02034553 40  -3.14591  0.0031
harmonic(month, 2, 12)3  0.019236 0.05206829 40   0.36944  0.7137
harmonic(month, 2, 12)4  0.027939 0.01995965 40   1.39977  0.1693
To:I(time - tpand)       0.006232 0.00806311 40   0.77286  0.4441
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.896                                            
To                       0.384 -0.605                                     
harmonic(month, 2, 12)1 -0.120  0.085 -0.016                              
harmonic(month, 2, 12)2  0.046 -0.062  0.146 -0.037                       
harmonic(month, 2, 12)3 -0.027 -0.034  0.188  0.045    -0.023             
harmonic(month, 2, 12)4  0.008  0.036 -0.072 -0.067    -0.001    -0.017   
To:I(time - tpand)       0.497 -0.525 -0.239 -0.015    -0.016    -0.189   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.012   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.05468068 -0.50819065 -0.03756207  0.47026019  2.40478815 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Dementia Level change 1.046 (0.851 to 1.285), p = 0.69
Sweden Dementia Trend change 1.006 (0.991 to 1.021), p = 0.444
Singapore Dementia Level change 1.354 (1.151 to 1.592), p = 0.001
Singapore Dementia Trend change 0.994 (0.972 to 1.017), p = 0.634
Peru Dementia Level change 1.682 (1.569 to 1.803), p < 0.001
Peru Dementia Trend change 1.016 (1.01 to 1.023), p < 0.001
Norway Dementia Level change 1.029 (0.965 to 1.098), p = 0.411
Norway Dementia Trend change 0.997 (0.992 to 1.001), p = 0.191
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.045:  log likelihood = -69.17
AIC=140.35   AICc=140.44   BIC=142.22
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.076156e-06 (Intr)
time        7.139642e-09 0     
Residual    1.121613e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.1916488 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.308514 0.05781445 40 -91.81986  0.0000
time                     0.005736 0.00359864 40   1.59404  0.1188
To                       0.121377 0.08391107 40   1.44649  0.1558
harmonic(month, 2, 12)1  0.027863 0.02892429 40   0.96331  0.3412
harmonic(month, 2, 12)2 -0.005173 0.02587891 40  -0.19988  0.8426
harmonic(month, 2, 12)3  0.005339 0.02903406 40   0.18389  0.8550
harmonic(month, 2, 12)4  0.005393 0.02585869 40   0.20855  0.8359
To:I(time - tpand)       0.002370 0.00615279 40   0.38519  0.7021
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.867                                            
To                       0.352 -0.602                                     
harmonic(month, 2, 12)1 -0.127  0.087 -0.041                              
harmonic(month, 2, 12)2 -0.034 -0.002  0.047 -0.002                       
harmonic(month, 2, 12)3  0.069 -0.121  0.252 -0.024     0.003             
harmonic(month, 2, 12)4  0.001  0.019 -0.030 -0.028    -0.025    -0.020   
To:I(time - tpand)       0.496 -0.577 -0.162  0.035     0.007    -0.115   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.030   

Standardized Within-Group Residuals:
         Min           Q1          Med           Q3          Max 
-2.709386442 -0.533177495 -0.006147322  0.595662627  1.991083512 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Dementia Level change 1.129 (0.967 to 1.318), p = 0.156
USA Dementia Trend change 1.002 (0.991 to 1.014), p = 0.702
Sweden Dementia Level change 1.046 (0.851 to 1.285), p = 0.69
Sweden Dementia Trend change 1.006 (0.991 to 1.021), p = 0.444
Singapore Dementia Level change 1.354 (1.151 to 1.592), p = 0.001
Singapore Dementia Trend change 0.994 (0.972 to 1.017), p = 0.634
Peru Dementia Level change 1.682 (1.569 to 1.803), p < 0.001
Peru Dementia Trend change 1.016 (1.01 to 1.023), p < 0.001
Norway Dementia Level change 1.029 (0.965 to 1.098), p = 0.411
Norway Dementia Trend change 0.997 (0.992 to 1.001), p = 0.191
China Dementia Level change 2.147 (0.75 to 6.145), p = 0.199
China Dementia Trend change 1.055 (0.95 to 1.171), p = 0.365
Canada Dementia Level change 1.457 (1.424 to 1.491), p < 0.001
Canada Dementia Trend change 1 (0.998 to 1.001), p = 0.904
Australia Dementia Level change 1.897 (1.788 to 2.012), p < 0.001
Australia Dementia Trend change 0.983 (0.98 to 0.987), p < 0.001
Argentina Dementia Level change 1.189 (0.767 to 1.843), p = 0.47
Argentina Dementia Trend change 0.978 (0.945 to 1.012), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.189 (0.767 to 1.843), p = 0.47

Trend change

0.978 (0.945 to 1.012), p = 0.238

Australia

Level change

1.897 (1.788 to 2.012), p < 0.001

Trend change

0.983 (0.98 to 0.987), p < 0.001

Canada

Level change

1.457 (1.424 to 1.491), p < 0.001

Trend change

1 (0.998 to 1.001), p = 0.904

China

Level change

2.147 (0.75 to 6.145), p = 0.199

Trend change

1.055 (0.95 to 1.171), p = 0.365

Norway

Level change

1.029 (0.965 to 1.098), p = 0.411

Trend change

0.997 (0.992 to 1.001), p = 0.191

Peru

Level change

1.682 (1.569 to 1.803), p < 0.001

Trend change

1.016 (1.01 to 1.023), p < 0.001

Singapore

Level change

1.354 (1.151 to 1.592), p = 0.001

Trend change

0.994 (0.972 to 1.017), p = 0.634

Sweden

Level change

1.046 (0.851 to 1.285), p = 0.69

Trend change

1.006 (0.991 to 1.021), p = 0.444

USA

Level change

1.129 (0.967 to 1.318), p = 0.156

Trend change

1.002 (0.991 to 1.014), p = 0.702

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Sleep Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.028:  log likelihood = -68.78
AIC=139.56   AICc=139.65   BIC=141.43
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.267062e-06 (Intr)
time        4.275108e-09 0     
Residual    1.330556e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.11311050  0.24984006  0.41710473 -0.06236869 -0.41430420 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.984804 0.07459115 40 -93.64119  0.0000
time                     0.010010 0.00435005 40   2.30101  0.0267
To                       0.424096 0.08775900 40   4.83250  0.0000
harmonic(month, 2, 12)1  0.018199 0.03885242 40   0.46841  0.6420
harmonic(month, 2, 12)2  0.120825 0.01347247 40   8.96832  0.0000
harmonic(month, 2, 12)3 -0.014538 0.03865408 40  -0.37611  0.7088
harmonic(month, 2, 12)4  0.042731 0.01325859 40   3.22288  0.0025
To:I(time - tpand)      -0.034375 0.00785735 40  -4.37492  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.901                                            
To                       0.337 -0.546                                     
harmonic(month, 2, 12)1 -0.050  0.027  0.018                              
harmonic(month, 2, 12)2  0.017 -0.100  0.240  0.143                       
harmonic(month, 2, 12)3  0.010 -0.026  0.196 -0.045     0.038             
harmonic(month, 2, 12)4 -0.037  0.069 -0.102 -0.112    -0.064     0.109   
To:I(time - tpand)       0.560 -0.632 -0.147  0.009    -0.035    -0.155   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1038711 -0.7774610 -0.2766568  0.7902937  2.1260996 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.065:  log likelihood = -69.62
AIC=141.24   AICc=141.32   BIC=143.11
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.324197e-07 (Intr)
time        4.140496e-09 0     
Residual    1.945804e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.04811765 -0.22137991  0.07746226 -0.04553207 -0.24503095 -0.33210074 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.492815 0.02323731 40 -236.37918  0.0000
time                     0.003787 0.00142666 40    2.65457  0.0113
To                       0.145081 0.03276823 40    4.42750  0.0001
harmonic(month, 2, 12)1 -0.029261 0.02444995 40   -1.19678  0.2384
harmonic(month, 2, 12)2  0.069948 0.01120997 40    6.23984  0.0000
harmonic(month, 2, 12)3  0.028167 0.02543119 40    1.10759  0.2747
harmonic(month, 2, 12)4 -0.040094 0.01126635 40   -3.55873  0.0010
To:I(time - tpand)      -0.011231 0.00219608 40   -5.11397  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.913                                            
To                       0.489 -0.698                                     
harmonic(month, 2, 12)1 -0.082  0.085 -0.099                              
harmonic(month, 2, 12)2 -0.021 -0.013  0.066  0.008                       
harmonic(month, 2, 12)3  0.126 -0.114  0.128 -0.011     0.045             
harmonic(month, 2, 12)4  0.004  0.022 -0.029 -0.017    -0.001     0.006   
To:I(time - tpand)       0.492 -0.501 -0.152  0.076    -0.012     0.000   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.39546354 -0.74476646 -0.02574487  0.76296003  2.18884296 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.048:  log likelihood = -69.22
AIC=140.45   AICc=140.53   BIC=142.32
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.991070e-07 (Intr)
time        3.249718e-09 0     
Residual    4.317011e-01       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.142128550 -0.040349914 -0.002853830 -0.270478260 -0.272741739 -0.305526321 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.002598161 -0.128557375 -0.140535724 -0.401501671 -0.137280831 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -6.303782 0.008248348 40 -764.2478  0.0000
time                    -0.003247 0.000535143 40   -6.0667  0.0000
To                       0.171792 0.014044859 40   12.2316  0.0000
harmonic(month, 2, 12)1 -0.003739 0.010037904 40   -0.3725  0.7115
harmonic(month, 2, 12)2  0.012350 0.006094405 40    2.0264  0.0494
harmonic(month, 2, 12)3 -0.022662 0.009794821 40   -2.3137  0.0259
harmonic(month, 2, 12)4 -0.014487 0.006049641 40   -2.3947  0.0214
To:I(time - tpand)       0.006452 0.000850782 40    7.5840  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.936                                            
To                       0.581 -0.748                                     
harmonic(month, 2, 12)1 -0.197  0.187 -0.147                              
harmonic(month, 2, 12)2 -0.043  0.028  0.004  0.012                       
harmonic(month, 2, 12)3  0.062 -0.077  0.166 -0.028     0.001             
harmonic(month, 2, 12)4 -0.010  0.012  0.018 -0.039    -0.040    -0.007   
To:I(time - tpand)       0.339 -0.294 -0.336  0.037    -0.008    -0.161   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.072   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9025822 -0.8322937 -0.3121056  0.8858935  2.4667662 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = FALSE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.306:  log likelihood = -74.52
AIC=151.04   AICc=151.12   BIC=152.91
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.401350e-06 (Intr)
time        8.512156e-09 0     
Residual    2.649526e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.8952508 -0.3376121  0.8093454 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + China_Dec20 + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -4.244657 0.11335693 38 -37.44506  0.0000
time                    -0.022308 0.01422392 38  -1.56835  0.1251
To                       0.510892 0.16933856 38   3.01699  0.0045
harmonic(month, 2, 12)1 -0.059697 0.06130160 38  -0.97382  0.3363
harmonic(month, 2, 12)2 -0.090549 0.04600912 38  -1.96808  0.0564
harmonic(month, 2, 12)3  0.066153 0.03972861 38   1.66512  0.1041
harmonic(month, 2, 12)4  0.025267 0.04381796 38   0.57664  0.5676
China_Jan19              1.361726 0.19621112 38   6.94011  0.0000
China_Dec20             -0.568116 0.21048163 38  -2.69913  0.0103
To:I(time - tpand)      -0.021539 0.01639522 38  -1.31372  0.1968
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.797                                            
To                       0.656 -0.817                                     
harmonic(month, 2, 12)1 -0.538  0.665 -0.679                              
harmonic(month, 2, 12)2 -0.276  0.353 -0.335  0.346                       
harmonic(month, 2, 12)3  0.238 -0.298  0.329 -0.181    -0.086             
harmonic(month, 2, 12)4  0.198 -0.245  0.218 -0.151    -0.078     0.061   
China_Jan19              0.515 -0.913  0.693 -0.610    -0.328     0.260   
China_Dec20              0.372 -0.462  0.633 -0.667    -0.360     0.138   
To:I(time - tpand)       0.262 -0.334 -0.124  0.165     0.080     0.033   
                        h(,2,12)4 Ch_J19 Ch_D20
time                                           
To                                             
harmonic(month, 2, 12)1                        
harmonic(month, 2, 12)2                        
harmonic(month, 2, 12)3                        
harmonic(month, 2, 12)4                        
China_Jan19              0.212                 
China_Dec20              0.058     0.425       
To:I(time - tpand)       0.113     0.304 -0.598

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.96812462 -0.60118059 -0.07952744  0.51488913  3.63309612 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Sleep Disorders Level change 1.667 (1.229 to 2.261), p = 0.005
China Sleep Disorders Trend change 0.979 (0.95 to 1.008), p = 0.197
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.6953
s.e.  0.1216

sigma^2 = 0.8326:  log likelihood = -63.54
AIC=131.07   AICc=131.34   BIC=134.82
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.458169e-06 (Intr)
time        1.362330e-08 0     
Residual    5.300849e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.5044051 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -4.873440 0.03631309 40 -134.20616  0.0000
time                     0.004795 0.00211896 40    2.26313  0.0291
To                       0.085402 0.04471238 40    1.91004  0.0633
harmonic(month, 2, 12)1  0.009682 0.01655855 40    0.58471  0.5620
harmonic(month, 2, 12)2  0.028070 0.01181381 40    2.37601  0.0224
harmonic(month, 2, 12)3  0.092114 0.01654445 40    5.56765  0.0000
harmonic(month, 2, 12)4  0.031888 0.01154604 40    2.76183  0.0086
To:I(time - tpand)      -0.011910 0.00394544 40   -3.01873  0.0044
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.857                                            
To                       0.243 -0.491                                     
harmonic(month, 2, 12)1 -0.088  0.041  0.008                              
harmonic(month, 2, 12)2 -0.033  0.004  0.100 -0.080                       
harmonic(month, 2, 12)3 -0.045 -0.043  0.240  0.004    -0.009             
harmonic(month, 2, 12)4 -0.041  0.074 -0.091 -0.046    -0.017    -0.093   
To:I(time - tpand)       0.523 -0.643 -0.174  0.038    -0.036    -0.163   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.048   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.63020852 -0.54585063  0.09267585  0.56738240  2.02737964 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Sleep Disorders Level change 1.089 (1.003 to 1.183), p = 0.063
Norway Sleep Disorders Trend change 0.988 (0.981 to 0.995), p = 0.004
China Sleep Disorders Level change 1.667 (1.229 to 2.261), p = 0.005
China Sleep Disorders Trend change 0.979 (0.95 to 1.008), p = 0.197
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.059:  log likelihood = -52.11
AIC=106.23   AICc=106.35   BIC=107.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.430648e-08 (Intr)
time        1.127635e-09 0     
Residual    2.778069e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6       Phi7 
-0.1849571 -0.4290715 -0.5244917 -0.4095390 -0.6113681 -0.3807415 -0.3283361 
      Phi8 
-0.1298395 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -8.390605 0.016474965 28 -509.2943  0.0000
time                     0.008756 0.001789061 28    4.8941  0.0000
To                       0.705928 0.017345641 28   40.6977  0.0000
harmonic(month, 2, 12)1 -0.025546 0.009781032 28   -2.6118  0.0143
harmonic(month, 2, 12)2  0.046000 0.014199017 28    3.2397  0.0031
harmonic(month, 2, 12)3 -0.042392 0.011091641 28   -3.8220  0.0007
harmonic(month, 2, 12)4 -0.017401 0.013788134 28   -1.2620  0.2173
To:I(time - tpand)      -0.027843 0.001831428 28  -15.2027  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.963                                            
To                       0.716 -0.835                                     
harmonic(month, 2, 12)1  0.070 -0.029 -0.013                              
harmonic(month, 2, 12)2 -0.040 -0.001 -0.008 -0.191                       
harmonic(month, 2, 12)3  0.413 -0.413  0.247 -0.022     0.085             
harmonic(month, 2, 12)4  0.015 -0.006  0.020  0.021    -0.019    -0.108   
To:I(time - tpand)       0.902 -0.919  0.592  0.042     0.049     0.479   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.019   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9804777 -0.4890623  0.1311510  0.7252228  2.1713010 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Sleep Disorders Level change 2.026 (1.963 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.973 (0.969 to 0.976), p < 0.001
Norway Sleep Disorders Level change 1.089 (1.003 to 1.183), p = 0.063
Norway Sleep Disorders Trend change 0.988 (0.981 to 0.995), p = 0.004
China Sleep Disorders Level change 1.667 (1.229 to 2.261), p = 0.005
China Sleep Disorders Trend change 0.979 (0.95 to 1.008), p = 0.197
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Sleep Disorders

Modeling Sleep Disorders for Singapore is not appropiate due to missing data in numerator.

Code
fake <- data.frame(x = seq(1, 48, 1), 
                   y = seq(1, 48, 1))

fake |> 
  ggplot(aes(x = x, y = y)) + 
  theme_void() + 
  annotate(geom = "text", 
           x = 24, 
           y = 24, 
           label = "Not calculable", 
           color = "black", 
           size = 6) -> plot7
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b, 
          shock_exclude = FALSE, 
          mean_cond = TRUE, 
          dum_outlier = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.042:  log likelihood = -69.1
AIC=140.19   AICc=140.28   BIC=142.06
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.212305e-07 (Intr)
time        2.308023e-09 0     
Residual    1.028689e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.03322689  0.12777293 -0.09228121 -0.20220267 -0.38080556  0.29025035 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.195730 0.04201351 40 -147.46996  0.0000
time                    -0.021831 0.00274040 40   -7.96648  0.0000
To                       0.461240 0.07157317 40    6.44431  0.0000
harmonic(month, 2, 12)1  0.108932 0.03162640 40    3.44434  0.0014
harmonic(month, 2, 12)2  0.045489 0.02962554 40    1.53546  0.1325
harmonic(month, 2, 12)3  0.182784 0.03359565 40    5.44070  0.0000
harmonic(month, 2, 12)4  0.005516 0.02907081 40    0.18976  0.8505
To:I(time - tpand)       0.001982 0.00516082 40    0.38406  0.7030
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.869                                            
To                       0.386 -0.627                                     
harmonic(month, 2, 12)1 -0.190  0.128 -0.095                              
harmonic(month, 2, 12)2 -0.064  0.052  0.020 -0.182                       
harmonic(month, 2, 12)3  0.083 -0.171  0.216  0.011    -0.128             
harmonic(month, 2, 12)4 -0.031  0.064 -0.049  0.051    -0.016    -0.168   
To:I(time - tpand)       0.403 -0.449 -0.278  0.048    -0.043    -0.039   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.053   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.82690950 -0.74078576 -0.04606689  0.73865294  2.58677655 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Sleep Disorders Level change 1.586 (1.39 to 1.81), p < 0.001
Sweden Sleep Disorders Trend change 1.002 (0.992 to 1.012), p = 0.703
Peru Sleep Disorders Level change 2.026 (1.963 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.973 (0.969 to 0.976), p < 0.001
Norway Sleep Disorders Level change 1.089 (1.003 to 1.183), p = 0.063
Norway Sleep Disorders Trend change 0.988 (0.981 to 0.995), p = 0.004
China Sleep Disorders Level change 1.667 (1.229 to 2.261), p = 0.005
China Sleep Disorders Trend change 0.979 (0.95 to 1.008), p = 0.197
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.3112
s.e.  0.1359

sigma^2 = 0.9593:  log likelihood = -66.66
AIC=137.31   AICc=137.58   BIC=141.06
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.454966e-06 (Intr)
time        8.232366e-09 0     
Residual    1.147776e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.3330002 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -4.424697 0.04645035 40 -95.25649  0.0000
time                    -0.013510 0.00306047 40  -4.41443  0.0001
To                       0.172465 0.07512029 40   2.29586  0.0270
harmonic(month, 2, 12)1  0.013427 0.02502992 40   0.53642  0.5946
harmonic(month, 2, 12)2 -0.040008 0.02031986 40  -1.96889  0.0559
harmonic(month, 2, 12)3  0.035110 0.02488268 40   1.41103  0.1660
harmonic(month, 2, 12)4 -0.003025 0.02033303 40  -0.14878  0.8825
To:I(time - tpand)       0.023900 0.00563334 40   4.24264  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.845                                            
To                       0.321 -0.578                                     
harmonic(month, 2, 12)1 -0.166  0.124 -0.052                              
harmonic(month, 2, 12)2 -0.051  0.023  0.038 -0.011                       
harmonic(month, 2, 12)3  0.024 -0.079  0.221 -0.017    -0.023             
harmonic(month, 2, 12)4 -0.003  0.027 -0.032 -0.026    -0.035    -0.035   
To:I(time - tpand)       0.470 -0.570 -0.199  0.004    -0.013    -0.141   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.6452082 -0.6778055 -0.1322661  0.6368199  2.0743336 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 3))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Sleep Disorders Level change 1.188 (1.034 to 1.365), p = 0.027
USA Sleep Disorders Trend change 1.024 (1.014 to 1.035), p < 0.001
Sweden Sleep Disorders Level change 1.586 (1.39 to 1.81), p < 0.001
Sweden Sleep Disorders Trend change 1.002 (0.992 to 1.012), p = 0.703
Peru Sleep Disorders Level change 2.026 (1.963 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.973 (0.969 to 0.976), p < 0.001
Norway Sleep Disorders Level change 1.089 (1.003 to 1.183), p = 0.063
Norway Sleep Disorders Trend change 0.988 (0.981 to 0.995), p = 0.004
China Sleep Disorders Level change 1.667 (1.229 to 2.261), p = 0.005
China Sleep Disorders Trend change 0.979 (0.95 to 1.008), p = 0.197
Canada Sleep Disorders Level change 1.187 (1.157 to 1.219), p < 0.001
Canada Sleep Disorders Trend change 1.006 (1.005 to 1.008), p < 0.001
Australia Sleep Disorders Level change 1.156 (1.088 to 1.228), p < 0.001
Australia Sleep Disorders Trend change 0.989 (0.985 to 0.993), p < 0.001
Argentina Sleep Disorders Level change 1.528 (1.3 to 1.797), p < 0.001
Argentina Sleep Disorders Trend change 0.966 (0.952 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.528 (1.3 to 1.797), p < 0.001

Trend change

0.966 (0.952 to 0.98), p < 0.001

Australia

Level change

1.156 (1.088 to 1.228), p < 0.001

Trend change

0.989 (0.985 to 0.993), p < 0.001

Canada

Level change

1.187 (1.157 to 1.219), p < 0.001

Trend change

1.006 (1.005 to 1.008), p < 0.001

China

Level change

1.667 (1.229 to 2.261), p = 0.005

Trend change

0.979 (0.95 to 1.008), p = 0.197

Norway

Level change

1.089 (1.003 to 1.183), p = 0.063

Trend change

0.988 (0.981 to 0.995), p = 0.004

Peru

Level change

2.026 (1.963 to 2.09), p < 0.001

Trend change

0.973 (0.969 to 0.976), p < 0.001

Sweden

Level change

1.586 (1.39 to 1.81), p < 0.001

Trend change

1.002 (0.992 to 1.012), p = 0.703

USA

Level change

1.188 (1.034 to 1.365), p = 0.027

Trend change

1.024 (1.014 to 1.035), p < 0.001

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Reproducibility Ticket

Code
sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=Spanish_Peru.utf8  LC_CTYPE=Spanish_Peru.utf8   
[3] LC_MONETARY=Spanish_Peru.utf8 LC_NUMERIC=C                 
[5] LC_TIME=Spanish_Peru.utf8    

time zone: America/Lima
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] imputeTS_3.3        flextable_0.9.1     parameters_0.21.0  
 [4] qqplotr_0.0.6       feasts_0.3.1        fabletools_0.3.3   
 [7] tsibble_1.1.3       DHARMa_0.4.6        zoo_1.8-12         
[10] tsModel_0.6-1       mgcv_1.8-42         MASS_7.3-58.4      
[13] forecast_8.21       nlme_3.1-162        lme4_1.1-33        
[16] Matrix_1.5-4        ggeffects_1.2.2     broom.mixed_0.2.9.4
[19] broom_1.0.4         gtsummary_1.7.1     janitor_2.2.0      
[22] DT_0.27             scales_1.2.1        patchwork_1.1.2    
[25] lubridate_1.9.2     forcats_1.0.0       stringr_1.5.0      
[28] dplyr_1.1.2         purrr_1.0.1         readr_2.1.4        
[31] tidyr_1.3.0         tibble_3.2.1        ggplot2_3.4.2      
[34] tidyverse_2.0.0     here_1.0.1          rio_0.5.29         
[37] pacman_0.5.1       

loaded via a namespace (and not attached):
  [1] splines_4.3.0           later_1.3.1             bitops_1.0-7           
  [4] cellranger_1.1.0        datawizard_0.7.1        xts_0.13.1             
  [7] lifecycle_1.0.3         doParallel_1.0.17       rprojroot_2.0.3        
 [10] globals_0.16.2          lattice_0.21-8          crosstalk_1.2.0        
 [13] insight_0.19.1          backports_1.4.1         magrittr_2.0.3         
 [16] sass_0.4.6              openxlsx_4.2.5.2        rmarkdown_2.21         
 [19] jquerylib_0.1.4         yaml_2.3.7              fracdiff_1.5-2         
 [22] qqconf_1.3.2            httpuv_1.6.10           zip_2.3.0              
 [25] askpass_1.1             minqa_1.2.5             multcomp_1.4-23        
 [28] quadprog_1.5-8          nnet_7.3-18             pracma_2.4.2           
 [31] TH.data_1.1-2           sandwich_3.0-2          gdtools_0.3.3          
 [34] listenv_0.9.0           crul_1.3                anytime_0.3.9          
 [37] parallelly_1.35.0       codetools_0.2-19        ggtext_0.1.2           
 [40] xml2_1.3.4              tidyselect_1.2.0        httpcode_0.3.0         
 [43] memuse_4.2-3            farver_2.1.1            urca_1.3-3             
 [46] broom.helpers_1.13.0    jsonlite_1.8.4          opdisDownsampling_0.8.2
 [49] ellipsis_0.3.2          survival_3.5-5          iterators_1.0.14       
 [52] emmeans_1.8.6           systemfonts_1.0.4       foreach_1.5.2          
 [55] tools_4.3.0             twosamples_2.0.1        ragg_1.2.5             
 [58] Rcpp_1.0.10             glue_1.6.2              xfun_0.39              
 [61] TTR_0.24.3              distributional_0.3.2    withr_2.5.0            
 [64] fastmap_1.1.1           boot_1.3-28.1           fansi_1.0.4            
 [67] openssl_2.0.6           caTools_1.18.2          digest_0.6.31          
 [70] timechange_0.2.0        R6_2.5.1                mime_0.12              
 [73] estimability_1.4.1      textshaping_0.3.6       colorspace_2.1-0       
 [76] utf8_1.2.3              generics_0.1.3          fontLiberation_0.1.0   
 [79] data.table_1.14.8       robustbase_0.99-0       httr_1.4.5             
 [82] htmlwidgets_1.6.2       pkgconfig_2.0.3         gtable_0.3.3           
 [85] timeDate_4022.108       lmtest_0.9-40           furrr_0.3.1            
 [88] htmltools_0.5.5         fontBitstreamVera_0.1.1 stinepack_1.4          
 [91] tseries_0.10-54         png_0.1-8               snakecase_0.11.0       
 [94] knitr_1.42              rstudioapi_0.14         tzdb_0.3.0             
 [97] uuid_1.1-0              coda_0.19-4             curl_5.0.0             
[100] nloptr_2.0.3            cachem_1.0.8            parallel_4.3.0         
[103] foreign_0.8-84          pillar_1.9.0            grid_4.3.0             
[106] vctrs_0.6.2             promises_1.2.0.1        xtable_1.8-4           
[109] evaluate_0.21           mvtnorm_1.1-3           cli_3.6.1              
[112] compiler_4.3.0          rlang_1.1.1             crayon_1.5.2           
[115] labeling_0.4.2          stringi_1.7.12          munsell_0.5.0          
[118] bayestestR_0.13.1       fontquiver_0.2.1        benchmarkme_1.0.8      
[121] hms_1.1.3               future_1.32.0           gfonts_0.2.0           
[124] shiny_1.7.4             haven_2.5.2             gridtext_0.1.5         
[127] gt_0.9.0                bslib_0.4.2             quantmod_0.4.22        
[130] benchmarkmeData_1.0.4   DEoptimR_1.1-0          readxl_1.4.2           
[133] officer_0.6.2